Fill a column automatically while you persist data to the database

If you want to fill a column automatically while you persist data to the database (e.g: slug) use Model Observer instead of hard code it every time

1use Illuminate\Support\Str;
2 
3class Article extends Model
4{
5 ...
6 protected static function boot()
7 {
8 parent:boot();
9 
10 static::saving(function ($model) {
11 $model->slug = Str::slug($model->title);
12 });
13 }
14}

Tip given by @sky_0xs

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord