我创建了一个事件侦听器,该事件侦听器在更新team_members
表之前应将该行保存到team_members_log
表中。当前,什么也没有发生,没有错误也没有插入。我不确定event
是否会被解雇。
我的模特,我正在从以下位置调度事件:
<?php
namespace App\Models;
use App\Events\TeamMemberUpdating;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class TeamMember extends Model
{
use Notifiable;
protected $table = 'team_members';
protected $fillable = ['project_request_id'];
protected $dispatchesEvents = [
'updating' => TeamMemberUpdating::class,
];
}
事件本身:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\TeamMember;
class TeamMemberUpdating extends Event
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $teamMember;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(TeamMember $teamMember)
{
$this->teamMember = $teamMember;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
/*public function broadcastOn()
{
return new PrivateChannel('channel-name');
}*/
}
听众:
<?php
namespace App\Listeners;
use App\Models\TeamMemberLog;
use App\Events\TeamMemberUpdating;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class LogTeamMemberWithProjectRequest
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param TeamMemberUpdating $event
* @return void
*/
public function handle(TeamMemberUpdating $event)
{
TeamMemberLog::create(['name' => $event->teamMember->name, 'title'=> $event->teamMember->title, 'picture' => $event->teamMember->picture, 'email' => $event->teamMember->email, 'phone_number' => $event->teamMember->phonenumber, 'background-information' => $event->teamMemmber->background_information ]);
}
}
注册它们:
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\TeamMemberUpdating' => [
'App\Listeners\LogTeamMemberWithProjectRequest',
],
];