Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Does every table need a model?

I am building an occasion system with Laravel. I have several tables like:

  • occasions
  • occasion_categories (cars, trikes and other)

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?

like image 715
user3527894 Avatar asked Jun 24 '14 01:06

user3527894


People also ask

Why do we need models in laravel?

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.

Can we get data from two tables in a single model in laravel?

@Jucedupp There's no way for a model to use multiple tables. You can go with relationships ( Order hasMany OrderItem , OrderItem belongsTo Order ).

Can laravel handle big data?

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.

Does laravel model have?

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.


1 Answers

Updated on 4th Oct, 2019:

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.

Original Answer:

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.

like image 97
The Alpha Avatar answered Oct 21 '22 17:10

The Alpha