Reputation: 285
I am building an occasion system with Laravel. I have several tables like:
Now i have a model called Occasion
which represents the table occasions, it also has a foreign key to the occasion_categories table.
I want to retrieve all occasion categories, but
do i need to make a seperated model called OccasionCategory
for the occasion_categories and define all relations in these models,
or can i just make a method in the Occasion
class like getCategories()
and then use the DB
class like DB::table('occasion_categories')->get()
to retrieve all possible categories?
Upvotes: 18
Views: 13521
Reputation: 146191
In short, NO
, you can use Query Builder
instead of Eloquent ORM
but if you want to use Eloquent ORM
then each table has to be bound to a model
. Also, a model is not necessarily has to be an Eloquent model
, you can create a model without extending eloquent model which may or may not use database. A model doesn't mean a database access layer but... anyways, it's another big topic.
Actually you need to create two Eloquent
models for both of your tables if you are using Eloquent
, for example:
class Occasion extend Eloquent {
// Laravel will look for occasions table for Occasion model so following line
// is optional if you don't want to use another table name for Occation model
protected $table = 'occasions';
// Now declare the relationship with "occasion_categories" table
public function occasionCategory()
{
// Make sure you have used occasion_categories_id as the foreugn key
return $this->belongsTo('OccasionCategory', 'occasion_categories_id', 'id');
}
}
Now create the OccasionCategory
model:
class OccasionCategory extend Eloquent {
protected $table = 'occasion_categories';
// Now declare the relationship with "occasion_categories" table
public function occasions()
{
// Make sure you have used occasion_categories_id as the foreign key
return $this->hasMany('Occasion', 'occasion_categories_id', 'id');
}
}
Now you may retrieve the occasions with it's parent occasions_category using something like this:
// Use the Occasion model
$allOccasionsWithCategory = Occasion::with('occasionCategory')->get();
// Find one Occasion whose id is 1 with OccasionCategory
$oneOccasionsWithCategory = Occasion::with('occasionCategory')->find(1);
// You may use this to get the related occasionCategory model
$occasionCategory = $oneOccasionsWithCategory->occasionCategory;
// Use the OccasionCategory model
$allOccasionsWithCategory = OccasionCategory::with('occasions')->get();
// Find one OccasionCategory whose id is 1 with Occasion
$oneOccasionsWithCategory = OccasionCategory::with('occasions')->find(1);
// You may use this to get all the related occasions model
$occasions = $oneOccasionsWithCategory->occasions;
Read more about relationship on Laravel
website.
If you use Query Builder
directly then you may use something like this (without a model):
// All occations
$occations = DB::table('occations')->get();
// All occasions and related occasion_categories
$occationsAndCategories = DB::table('occations')
->join('occasion_categories', 'occasions.occasion_category_id', '=', 'occasion_categories.id')
->get();
Read more about Query Builder on Laravel
website.
Upvotes: 16
Reputation: 211
Just wanted to chime in here with an update.
In this particular instance, I'm not sure things have changed since this was posted (could be wrong, always more to discover), but in the case of Many-to-Many relationships, the one-to-one relationship between table and model is broken, thanks to the pivot() method .
Taking from one of my own projects:
public function fees()
{
return $this->belongsToMany(Service::class)->withPivot("description");
}
In this way, you can have two models ("Fee" and "Service"), but you can access data on the intermediary table without needing a "ServiceFee" model.
In Blade:
{{ $service->pivot->description }}
At a glance, this may seem trivial, but get enough many-to-many relationships going and the number tables could even surpass the number of models. (Whether this hypothetical scenario is necessarily well-designed is a matter I am sadly lacking the time to ponder.)
Upvotes: 2
Reputation: 7575
The laravelish way to do database is using the Query Builder or Eloquent: Laravel is a framework and it always makes sense to leverage the resources it exposes.
The short answer:
- No, it doesn't as long as you don't use Eloquent.
The long answer:
The Query Builder is the way to go if you still want to stick to laravel convention.
You may also use the plain old PDO: Remember Laravel is a actually a PHP framework!
It's always rewarding to be consistent:
Do it the Eloquent
ways (Models + relationships definition) despite you can mix Eloquent
and some Query Builder
as you suggested.
Doing it using Query Builder
exclusively is also consistent and possible.
My personal preference goes to the Eloquent
option.
WereWolf - The Alpha answer provides excellent possible solutions using both options.
Upvotes: 13
Reputation: 817
Although I am not positive, I am pretty sure that would work. Or, DB::raw('select * from occasion_categories')
should work.
I don't know what the best practices here are, but if you're simply asking if it's possible then yes. One of those two methods should work just fine.
Upvotes: 0