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?
Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.
@Jucedupp There's no way for a model to use multiple tables. You can go with relationships ( Order hasMany OrderItem , OrderItem belongsTo Order ).
Laravel supports Eloquent, which is one of PHP's robust frameworks for data handling, gives you faster access to the data. Laravel contains lighter templates and widgets such as JS and CSS code that are actively being used to manage Big Data.
has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With