Interview GURU

How will you explain Guarded Attribute in a Laravel model?

The guarded attribute is the opposite of fillable attributes.

In Laravel, fillable attributes are used to specify those fields which are to be mass assigned. Guarded attributes are used to specify those fields which are not mass assignable.

Code Source

class User extends Model {  
protected $guarded = ['role'];   
// All fields inside the $guarded array are not mass-assignable  
}  

If we want to block all the fields from being mass-assigned, we can use:

protected $guarded = ['*'];  

$fillable serves as a "white list" whereas $guarded functions serves like a "black list". One should use either $fillable or $guarded.