Some Laravel Model Properties Which You Need To Know

Hello artisan,

in this tutorial, I will discuss some model attributes in Laravel. If you are working with MySQL then we do not need to change many things about a table. But if you work with mssql server then you may need to know about everything about a model in Laravel. You know that Laravel is MySQL friendly but not mssql. 

Primary Keys

Laravel Eloquent assumes that each model's corresponding database table has a primary key column named id. But assume your table has no id, there is a different primary key, then you can define or you can tell Laravel that, Hey Laravel, this is my table primary key. Please set it.

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Flight extends Model
{
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'flight_id';
}

 

There is a another features for primary key like If you wish to use a non-incrementing or a non-numeric primary key you must define a public $incrementing property on your model that is set to false:

class Flight extends Model
{
    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;
}

 

Assume your model's primary key is not an integer, you should define a protected $keyType property on your model. This property should have a value of string:

class Flight extends Model
{
    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';
}

 

Timestamps

By default, Laravel Eloquent model expects created_at and updated_at columns to existing on your model's corresponding database table. Eloquent will automatically set these columns' values when models are created or updated. Suppose, we do not want these columns to be automatically managed by Eloquent, you should define a $timestamps property on your model with a value of false:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Flight extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
}

 

If you need to customize the format of your model's timestamps, set the $dateFormat property on your model. 

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

 

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

class Flight extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'updated_date';
}

 

Database Connections

If we would like to connect a different connection that should be used when interacting with a particular model, you should define a $connection property on the model:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Flight extends Model
{
    /**
     * The database connection that should be used by the model.
     *
     * @var string
     */
    protected $connection = 'sqlite';
}

 

Default Attribute Values

Assume we would like to define the default values for some of your model's attributes, you may define an $attributes property on your model:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Flight extends Model
{
    /**
     * The model's default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'delayed' => false,
    ];
}

 

Allowing Mass Assignment

Assuming, we would like to make all of your attributes mass assignable, you may define your model's $guarded property as an empty array. If we choose to unguard your model, you should take special care to always hand-craft the arrays passed to Eloquent's fillcreate, and update methods:

/**
 * The attributes that aren't mass assignable.
 *
 * @var array
 */
protected $guarded = [];

 

Read also: Some Laravel Best Practices Every Developer Should Know

 

Casts

We can change attribute types like, assume properties is column of a table:

protected $casts = [
   'properties' => 'array'
];

 

Appends

We can append a custom row with our eloquent results like:

protected $appends = ['editLink'];

public function getEditLinkAttribute()
{
  return route('product.edit', $this);
}

 

Now it will attach edit_link with your eloquent query result.

 

#laravel #laravel-9x