Jay Momaya
Jay Momaya

Reputation: 2049

What is actually model in laravel and where it is used?

I do not understand what a model and Eloquent are. I tried this code

<?php

    class Nerd extends Eloquent
    {

    }

But still not getting what this is. What does the modeling do?

Upvotes: 2

Views: 11553

Answers (4)

sabuz
sabuz

Reputation: 849

Step by step discussed here

Eloquent ORM (object relational Mapping) is included with Laravel and provides an extra layer for working with your database. Usually, we directly interact or make queries to a table in the database directly. That's why we write code like the following with php and mysql:

select * from table_name

Laravel also provides a query builder to directly interact with tables. Suppose a table name is users:

   DB::table('users')->select('*')->get();

Eloquent provides an extra layer to interact with tables by creating a corresponding "model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table. Once a model is made, all interactions with the table will be completed through the model.

For example:

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
//
}

Here the User model represents the users table. Model name is singular and its corresponding table name will be plural.

and if all user data is needed, the Eloquent query will be

$users= App\User::all();

and if a namespace is used inside a controller (where the Eloquent query will live)

use App\User;
$users = User::all(); //here no need to give App namespace like the above code

Upvotes: 18

user7963297
user7963297

Reputation:

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. Models allow you to query for data in your tables, as well as insert new records into the table.

Upvotes: 2

Ntwari Joshua
Ntwari Joshua

Reputation: 11

Laravel is an MVC base framework the Model is the M in the MVC pattern which maps your classes that extend Illuminate\Database\Eloquent\Model to your database tables and manages all your database functionality. the models in laravel are the in the app/ directory and the can be created using an artisan command "php artisan make:model ModelName" then they can later on be used in your controllers to interact with the database

Upvotes: 1

Divyank Munjapara
Divyank Munjapara

Reputation: 184

There is no difference between Eloquent and Model, if you see config/app.php file you will find this alias

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

mease when you use "Eloquent", then it will refer to Model class and if you use "Model" then also it will refer to same class.

Upvotes: 1

Related Questions