1# If you would like to generate a database migration when you
2# generate the model, you may use the --migration or -m option:
3
4php artisan make:model Flight --migration
5php artisan make:model Flight -m
1make observer by follow command in Artisan:
2----------------------------------------------
3 php artisan make:Observer ProductObserver --model=<put here path of your Model>
4
5
6then in app/providers/EventServiceProvider.php at boot Methode write follow code:
7----------------------------------------------------------------------------------
8 public function boot()
9 {
10 Product::observe(ProductObserver::class);
11 }
12
13
1retrieved
2creating
3created
4updating
5updated
6saving
7saved
8deleting
9deleted
10restoring
11restored
1namespace App;
2
3use Illuminate\Database\Eloquent\Model;
4
5class Post extends Model
6{
7 protected $table = 'posts';
8
9 protected $fillable = ['title', 'slug', 'content'];
10
11 protected static function boot()
12 {
13 parent::boot();
14 static::saving(function ($model) {
15 $model->slug = str_slug($model->title);
16 });
17 }
18}
1<?php
2
3namespace App\Observers;
4
5use App\Models\User;
6
7class UserObserver
8{
9 /**
10 * Handle the User "created" event.
11 *
12 * @param \App\Models\User $user
13 * @return void
14 */
15 public function created(User $user)
16 {
17 //
18 }
19
20 /**
21 * Handle the User "updated" event.
22 *
23 * @param \App\Models\User $user
24 * @return void
25 */
26 public function updated(User $user)
27 {
28 //
29 }
30
31 /**
32 * Handle the User "deleted" event.
33 *
34 * @param \App\Models\User $user
35 * @return void
36 */
37 public function deleted(User $user)
38 {
39 //
40 }
41
42 /**
43 * Handle the User "forceDeleted" event.
44 *
45 * @param \App\Models\User $user
46 * @return void
47 */
48 public function forceDeleted(User $user)
49 {
50 //
51 }
52}