What Are The Basic Usage Of Models In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

In Laravel, Model is a class that represents the logical structure and relationship of underlying data table. In Laravel, each of the database table has a corresponding “Model” that allow us to interact with that table. Models gives you the way to retrieve, insert, and update information into your data table

What Are The Basic Usage Of Models In Laravel Framework

Basic usage importance of Model in laravel.

1.Create a model instance is using the make:model Artisan command:

php artisan make:model demo


Now, let's look at an example demo model, which we will use to retrieve and store information from our demo database table:

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class demo
 * @package App\Models
 * @version September 03, 2020, 6:44 pm UTC
 *
 * @property string name
 * @property string desc
 */
class demo extends Model
{
   //
}
 

2.Table Names

Note that we did not tell Eloquent which table to use for our demo model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the demo model stores records in the demos table. You may specify a custom table by defining a table property on your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'demos';
}


3.Primary Keys
Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $primaryKey = 'demo_id';

}
 

In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will automatically be cast to an int. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    public $incrementing = false;
}


If your primary key is not an integer, you should set the protected $keyType property on your model to string:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $keyType = 'string';
}
 

4.Timestamps
By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    public $timestamps = false;
}
 


If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}


If you need to customize the names of the columns used to store the timestamps, you may set the  CREATED_AT and UPDATED_AT  constants in your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';
}


 

5.Database Connection
By default, all Eloquent models will use the default database connection configured for your application. If you would like to specify a different connection for the model, use the $connection property:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $connection = 'connection-name';
}

Related Post