Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whats the difference between fillable and guard in laravel?

I am newbie in Laravel and want to understand this with example. what are main difference between fillable and guard in laravel? How those are differentiated? Please share one basic example.

like image 758
stephenn Avatar asked Sep 21 '16 12:09

stephenn


People also ask

What is guarded and fillable?

$guarded attribute is used to specify those fields which are to be made non mass assignable. $fillable serves as a "white list" of attributes that should be mass assignable and $guarded acts just the opposite of it as a "black list" of attributes that should not be mass assignable.

What is fillable in laravel?

The fillable property is used inside the model. It takes care of defining which fields are to be considered when the user will insert or update data. Only the fields marked as fillable are used in the mass assignment. This is done to avoid mass assignment data attacks when the user sends data from the HTTP request.

What is fillable array in laravel?

In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.

What is protected in laravel?

Laravel use protected to protect you from a breach into your database.. Like an Sql injection. It also contains statements that escape any threat a user will pass through your forms. Follow this answer to receive notifications.


1 Answers

Example 1

protected $fillable = ['name', 'email'];

It means we want to insert only name,and email colmn values

Example 2

protected $guarded = ['name', 'email'];

It means we want to ignore only name & email we don't want to insert values of name & email colmn

Example 3

protected $guarded = [];

We want to insert all columns values

like image 196
Adeel Ahmed Baloch Avatar answered Oct 11 '22 16:10

Adeel Ahmed Baloch