Eloquent ORM
About Eloquent ORM

In Eloquent ORM, 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, Delete record, update record into the table.

To Create Model

php artisan make:model cakemodel

Note : where cakemodel is my model name

Be default model create in app folder, For best practice, create Models folder inside app folder then create Model.

Note: Try to take model name should be same as database table name. otherwise there is necessary to take another variable that store class name like

protected $table = 'myusers';

where myusers in table name

Create Model in App folder

php artisan make:model cakemodel

namespace App;
use Illuminate\Database\Eloquent\Model;

class cakemodel extends Model

//
}

Create model inside Models folder

php artisan make:model Models\mycake

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class mycake extends Model
{
//
}

Laravel In Built Model (User Model)

<?php

n

namespace App;

n

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

n

class User extends Authenticatable
{
use Notifiable;

n

protected $fillable = [
'name', 'email', 'password',
];

n


protected $hidden = [
'password', 'remember_token',
];
}

n

In the above code we see User Model that is extends User class here both class(base and derived class) same show we take base user class as alisa. And User is table in my Database.


Previous
Next