Home> PHP Framework> Laravel> body text

Detailed explanation of the two ways of Laravel model events

藏色散人
Release: 2021-07-26 09:11:17
forward
2681 people have browsed it

When dealing with some user operation events on a daily basis, we sometimes need to record them , for later reference or big data statistics.


Laravel is very convenient to handle in model events: https://laravel-china.org/docs/laravel/5.5/eloquent#events


Laravel’s model There are two ways of events,

  • SettingdispatchesEventsProperty mapping event class
  • Use observers to register events, here is the second one
  • New model

php artisan make:model Log


         
Copy after login
  • Create migration table:

php artisan make:migration create_logs_table

  • The structure of the table is roughly like this, it can be designed as needed
engine = 'InnoDB'; $table->increments('id'); $table->string('user_id')->comment('操作人的ID'); $table->string('user_name')->comment('操作人的名字,方便直接查阅'); $table->string('url')->comment('当前操作的URL'); $table->string('method')->comment('当前操作的请求方法'); $table->string('event')->comment('当前操作的事件,create,update,delete'); $table->string('table')->comment('操作的表'); $table->string('description')->default(''); $table->timestamps(); }); DB::statement("ALTER TABLE `logs` comment '操作日志表'"); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('logs'); } }
Copy after login
  • Run the migration to generate the table

php artisan migrate

  • Create a new service provider to uniformly register all model event observers (the subsequent names can be more vivid)

php artisan make:provider ObserverLogServiceProvider

  • to theprovidersarray in/config/app.phpRegister (roughly as shown in the picture)

Detailed explanation of the two ways of Laravel model events

    ##Create a new folder in the
  • appdirectoryObserversStore model observers, and create a new base classLogBaseServerand build basic attributes in the constructor (CLI is because there is no user execution when executing from the command line)

Detailed explanation of the two ways of Laravel model events

##Create a new observer inheriting the base class
    LogBaseServer
  • (Usermodel, method The name should correspond to the event in the document)

Detailed explanation of the two ways of Laravel model events##To the newly created service provider

ObserverLogServiceProvider
    Run in

Detailed explanation of the two ways of Laravel model eventsRegister events for the required models (I have quite a few, it will probably look like this in the future )

Detailed explanation of the two ways of Laravel model eventsThen we trigger some events (addition, deletion and modification, the table data will be available)

Detailed explanation of the two ways of Laravel model events##Many-to-many association insertion will not trigger the model (such as

attach
Method)
  • At this time, you need to create a new event class to simulate (here is a rough introduction to assigning permissions to roles)
  • 1. In
  • EventServiceProvider
listen

The attribute is bound to the event

2. Injection two in the eventDetailed explanation of the two ways of Laravel model eventsPermissionRoleEventParameters, one is the role, the other is the array returned by

attach

ordetach

##

3. Event listenerPermissionRoleEventLogalso inherits the base classLogBaseServer, here it is traversed according to the incoming array id, and then creates a log

Detailed explanation of the two ways of Laravel model events

4. Then apply the event

Detailed explanation of the two ways of Laravel model events


  • Update Handle login and logout events gracefully

1. Bind thesubscribeattribute inEventServiceProviderto a well-handled class

Detailed explanation of the two ways of Laravel model events

2. Methods of the event listening class

Detailed explanation of the two ways of Laravel model events

3. After The effect is like this:

Detailed explanation of the two ways of Laravel model events

#Related recommendations:The latest five Laravel video tutorial

#

The above is the detailed content of Detailed explanation of the two ways of Laravel model events. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!