observer laravel after create code example

Example 1: create observer in laravel

php artisan make:observer nameOfObserver --model=modelName

Example 2: laravel create observer

php artisan make:observer UserObserver --model=User

Example 3: model observer laravel

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts';

    protected $fillable = ['title', 'slug', 'content'];

    protected static function boot()
    {
        parent::boot();
        static::saving(function ($model) {
            $model->slug = str_slug($model->title);
        });
    }
}