laravel Observer code example
Example 1: create observer in laravel
php artisan make:observer nameOfObserver --model=modelName
Example 2: laravel delete where
DB::table('users')->where('id', $id)->delete();
Example 3: laravel observer
php artisan make:observer UserObserver --model=User
Example 4: laravel create model and migration
php artisan make:model Flight --migration
php artisan make:model Flight -m
Example 5: laravel observer
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
public function created(User $user)
{
}
public function updated(User $user)
{
}
public function deleted(User $user)
{
}
public function forceDeleted(User $user)
{
}
}
Example 6: laravel observer
<?php
namespace App\Providers;
use App\Observers\UserObserver;
use App\Models\User;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
}
public function boot()
{
User::observe(UserObserver::class);
}
}