laravel-事件

1.App\Providers\EventServiceProvider文件中添加事件和监听器
protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'App\Events\UserLogin' => [
            'App\Listeners\SendSms',
        ],
    ];
2.php artisan event:generate 生成事件和监听器文件
3.编辑事件文件 App\Events\UserLogin
public function __construct(User $user)
    {
        //
        $this->user=$user;
    }
4.编辑监听器文件 App\Listeners\SendSms
public function handle(UserLogin $event)
    {
        //
        Log::info($event->user->toArray());
    }
5.业务代码中触发事件
$user=User::find(8);
        event(new UserLogin($user));

 

posted @ 2021-09-26 15:41  CanyingV  阅读(18)  评论(0编辑  收藏  举报