What is Model

In MVC architectural pattern, the letter ‘M’ stands for Model. Model represents the data structure and it does not depend on the controller or the view. It directly connects with the database and represents the data structure of a particular table.

How to create model in Laravel

Go to Command Line from your Laravel project directory and write this command to create a new model.
php artisan make:model ModelName

Suppose, you want to create a model named User. Then you will have to write:
php artisan make:model User

In Laravel 5, Models are stored in the app/ directory.

Using a model

Suppose, you want to access users table of your database. Then you want to create, read, update and delete records from the users table. To do this, you need to create a model. You can choose any name that you want. But according to Laravel’s naming convention, the model’s name has to be the singular form of the table’s name. So as our table’s name is users, so we will create a model with the name User. But, of course, you don’t have to follow the naming convention if you don’t want. If your table’s name is different, then in the model, you can write:

protected $table = 'tableName';

Your model will contain the data structure of the specific table and the logic code for relations between different tables. Here’s an example:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['first_name', 'last_name', 'email', 'password', 'dob'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    protected $appends = ['dob_dmy','age','full_name', 'advisor_name', 'advisor_email'];

    protected $dates = ['deleted_at'];

    // Join with profile_picture table
    public function profile_picture()
    {
        return $this->belongsTo('App\ProfilePicture','id','user_id');
    }

    // Join with advisors table
    public function advisor()
    {
        return $this->belongsTo('App\Advisors','advisor_code','agent_code');
    }

    // Join with profile_picture table
    public function rooms()
    {
        return $this->hasMany('App\Rooms','user_id','id');
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>