Using fillable or guarded

If you have Post Model with title and description you can setup these fields with fillable or gurded in order to save these with create or update method in controller so that mass assignment can be protected.

// In Model
protected $fillable = ['title','description']
//or
protected $guarded = ['id'] // only id field is set to guarded so that other fields are allowed

//In Controller
Article::create($request->all());
Article::update($request->all());


If fillable or guarded is not used then In controller saving or updating would be like following

// In Controller
$artcile = new Article();
$article->title = request('title');
$article->description = request('description');
$article->save(); 

$artcile = Article::firstOrFail($articleId);
$article->title = request('title');
$article->description = request('description');
$article->save(); 

Related Posts


Changing timestamp field names

Setting up custom date format