Home>Article>Backend Development> Learn several uses of laravel's model events
This article mainly introduces you to the relevant information about the usage of model events in laravel study notes. The article introduces it in great detail through example code. It has certain reference learning value for everyone to learn or use laravel. Friends who need it can refer to it. For reference, let’s learn with the editor below.
Preface
This article mainly introduces you to the relevant content about the usage of laravel model events. The article introduces the laravel model events through sample code. There are many ways to use them. I won’t go into details below. Let’s take a look at the detailed introduction.
Usage examples
1. Simple and rude (for local testing)
Definition in routing:
Event::listen('eloquent.updated: App\Post',function (){ dump('测试一下修改事件'); }); Route::post('/post/{id}', 'PostController@update');
2. Generate events and listeners
Define the corresponding in EventServiceProvider Relationship
protected $listen = [ 'App\Events\PostEvent' => [ 'App\Listeners\PostListener', ], ];
php artisan event:generate //生成文件
#event Inject the class to be operated
listen Inject the corresponding event class into the handle method
public function handle(PostEvent $event) { dump('测试一下修改事件'); }
Finally add the 'events' attribute to the post model
protected $events = [ 'updated' => PostListener::class ];
3. Use the boot method of the framework
Define directly in the relevant Model
public static function boot() { parent::boot(); static::updated(function($model) { dump('测试一下修改事件'); }); }
4. Define Trait
If you want to perform some operations on the updated or created events of multiple models, you should not do it separately for each model. Write one. For example: Log.
trait LogRecord { //注意,必须以 boot 开头 public static function bootLogRecord() { foreach(static::getModelEvents() as $event) { static::$event(function ($model){ $model->setRemind(); }); } } public static function getModelEvents() { if(isset(static::$recordEvents)){ return static::$recordEvents; } return ['updated']; } public function setRemind() { dump('记录逻辑操作'); } }
Then, just use trait in the model.
• creating - the object is ready but has not been written to the database
• created - the object has been written to the database
• updating - the object has been modified but has not been written to the database
• updated - the modification has been written to the database
• saving - the object was created or updated but not written to the database
• saved - the object Create or update the database that has been written
• deleting - before deletion
• deleted - after deletion
• restoring - restore soft deletion Before
• restored - After restoring soft deletion
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Learning about transferring arrays to views in the Laravel5 framework
Models and controllers of the Laravel 5 framework And learning the basic process of views
The above is the detailed content of Learn several uses of laravel's model events. For more information, please follow other related articles on the PHP Chinese website!