Model
What is Model

In Laravel Model is Used for database table, this is called Eloquent ORM.

How to create model

In Laravel there are no any model folder, we can create model folder any where.Best place is app/models. 
Be Default In Laravel we have a User Model. In Laravel5.6 we see model in app->user.php

How to Create New Model?

First Create Model folder in Laravel suppose we create in aap/models(Location is not mandatory)

<?php

namespace AppModels;

use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use DB;

class Admin extends Model
{

protected $table = 'admin';

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

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

protected $dates = ['deleted_at'];


}

Note: Suppose admin is table in my database so the above model is for admin table. In the above code Admin class is create for admin table and column of the admin table will be a properties of this class object and this object is also provide as save() method etc.

Laravel User Model

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

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


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

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