Laravel 10.xのartisanの「make:observer」コマンドを解説します。
Description
新しいオブザーバークラスを作成します。
Usage
php artisan make:observer [-f|--force] [-m|--model [MODEL]] [--] <name>
Arguments
引数 | 必須 | 設営 |
---|---|---|
name | 必須 | オブザーバークラス名 |
Options
オプション | 省略形 | 必須 | 説明 |
---|---|---|---|
force | f | - | オブザーバーが既に存在する場合でもクラスを作成する |
model | m | - | オブザーバーが適用するモデル |
Examples
no options
php artisan make:observer FooObserver
INFO Observer [app/Observers/FooObserver.php] created successfully.
<?php
namespace App\Observers;
class FooObserver
{
//
}
model
php artisan make:observer -model=User FooObserver
INFO Observer [app/Observers/FooObserver.php] created successfully.
<?php
namespace App\Observers;
use App\Models\User;
class FooObserver
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
//
}
/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
//
}
/**
* Handle the User "deleted" event.
*/
public function deleted(User $user): void
{
//
}
/**
* Handle the User "restored" event.
*/
public function restored(User $user): void
{
//
}
/**
* Handle the User "force deleted" event.
*/
public function forceDeleted(User $user): void
{
//
}
}
Stub
Option | Stub file name |
---|---|
- | observer.stub |
model | observer.plain.stub |
Source
10.x
framework/src/Illuminate/Foundation/Console/ObserverMakeCommand.php at 10.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
11.x
framework/src/Illuminate/Foundation/Console/ObserverMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
コメント