Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between an Eloquent Model and a Model?

So, i'm confused with this : In Laravel's official documentation, they say :

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table.

Ok util here all is Great, i get it !

So I make a migration to create a database : php artisan make:migration create_items_table --create="items"

Great until here too :)

So theoretically speaking, when i will make : php artisan make:model Item , Laravel will create a php class ( which is used to interact with items table) :

   class Item extends Eloquent {

     ...

    }

But,in real world, when i make : php artisan make:model Item , Laravel creates php class ( which is used to interact with items table) ::

   class Item extends Model {

     ...

    }

So Why Model and not Eloquent ?? Am i missing sth ? And What's the difference between Eloquent Model and Model.

And if there's a difference, when should i use Eloquent and when Model ... ?

Thank you ^^

like image 346
elarib Avatar asked Jul 13 '15 10:07

elarib


2 Answers

Eloquent is the name given to the ORM (Object-relational mapping) that ships with Laravel. Eloquent allows you to interact with your tables as though they were objects, however Eloquent is unaware of the actual columns you have on your table.

Let's consider the simple User model. We want this model to query records on our users table.

class User extends Eloquent {

protected $table = 'users';

} That there is a very simple model. Now, instead of querying like this.

$user = DB::table('user')->find(1);

You can query like this.

$user = User::find(1);

Eloquent itself uses its own query builder but does fall back to the standard query builder. This means it has all the methods on the query builder available to it, and more.

The benefits here are:

  • You don't have to specify your table name on every call.
  • The code reads a whole lot better, it's syntactical sugar.
  • You can create complex relationships between tables and use eager loading.
  • You can make use of functionality such as mass assignment protection and setters/getter.

I've only touched on Eloquent. There is so much more to it. I suggest you take a look at the following resources.

  • Eloquent Documentation
  • Dayle Rees' Code Happy

For more exact simple example click here

like image 83
Jigs Virani Avatar answered Oct 22 '22 13:10

Jigs Virani


I found the solution for my question ...

So Normally you must add an alias to your config/app.php

 'aliases' => [
        'Eloquent'  => Illuminate\Database\Eloquent\Model::class,

And when you create a model, Laravel will use Eloquent; instead of use Illuminate\Database\Eloquent\Model; this why i think, some person may use Model, when other may use Eloquent , it's just a matter of give a meaning sense to the namespace :3

like image 33
elarib Avatar answered Oct 22 '22 13:10

elarib