Laravel eloquent guarded vs fillable

fillable vs guarded

Fillable vs Guarded

Fillable and Guarded attributes are used to work with the Laravel Eloquent ORM system to create the possibilities for the mass-assignable fields restrictions and specifications in terms of protecting data from unwanted access and manipulation by users.

What is Fillable?
Fillable lets you specify which fields are mass-assignable in your model. Let’s take the above example, you can do it by adding the special variable $fillable to the model. So in the model:

class Student extends Model {
  protected $fillable = ['first_name', 'last_name', 'email']; // only the field names inside the array can be mass-assign
}


What is Guarded?
Guarded is the reverse of fillable. If fillable specifies which fields to be mass assigned, guarded specifies which fields are not mass assignable. So in the model:

class Student extends Model {
  protected $guarded = ['id', 'student_type']; // the field name inside the array is not mass-assignable
}

protected $guarded = ['*']; // block all the fields from being mass-assigned

protected $guarded []; // make all the fields mass assignable

 

For more information about fillable vs guarded, you can check Laravel’s official website here.

You can find more articles about Laravel here.

Please follow and like us:

Related Posts

Leave a Reply

Share