In this tutorial, we will discuss laravel eloquent model all configurations according to conditions these are done in the tables model
laravel model configurations.
- table
- fillable
- guarded
- primaryKey
- incrementing
- keyType
- timestamps
- timestamp column name
- attribute default name
How to customize table name in laravel
laravel rule is table name in a plural form and related model its singular form if over model is not follow these rule than we give table name in $table variable which is protected type let an example over model is 'Student' and table name is 'school_students' than it gives an error if we do not define in model.
protected $table = 'school_students';
Why we use $fillable in laravel
the fillable attribute is used when we mass data store in table throw model. Fillable is array type variable that assigns values that are mass assignable
protected $fillable = [ 'name', 'email', 'password', ];
Use of laravel $guarded property
guarded attribute is just opposite of fillable attribute it takes attribute name which is not mass assignable guarded and fillable both are use when we mass data assign like when we use to create() method in the controller to create data than it gives an error if we do not use anyone mass assign configuration
protected $guarded = ['role'];
primaryKey configuration in laravel model
How to use different name primary key instead of id then we use this model configuration in this we give a primary key name in $primaryKey variable let our 'students' table primary key name 'student_id'.
protected $primaryKey = 'student_id';
When we not use auto increment id column in laravel table
how to prevent auto increment column from a table in laravel then we use this configuration if in our table no autoincrement column found than pass $incrementing variable false.
public $incrementing = false;
Laravel model keyType property
If in our table primary key not have integer type than we pass $keyType string.
protected $keyType = 'string';
If we not use created_at and updated_at column in laravel table
If we not define created_at and updated_at column in over table than we pass false in $timestamps variable.
public $timestamps = false;
How to change timestamp column name in laravel table
how to change created_at and updated_at column name in laravel table then we use this configuration let our created_at column name start_date than we use.
const CREATED_AT = 'start_date';
Set default value configuration of a column in laravel model
how to define a table column default value by laravel eloquent model than we pass array of columns and its values in $attributes variable.
protected $attributes = [ 'name' => 'default name', ];
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- What is laravel eloquent model and introduction
- part 1 laravel eloquent model all configuration hindi
- part 2 laravel insert and create data by eloquent model tutorial
- part 3 laravel data retrieve method
- part 4 first, find, findOrFail and firstOrFail method
- part 5 laravel delete vs truncate method
- part 6 laravel save vs update method
- part 7 laravel updateOrCreate and upsert
- part 8 laravel chunk vs cursor method
- part 9 laravel eloquent subquery select
- part 10 laravel findOrFail vs firstOrFail
- part 11 laravel create and retrieve data
- part 12 laravel eloquent aggregate functions
- part 13 laravel isDirty vs isClean vs wasChanged method