雄辩的事件删除监听器返回false会停止删除功能吗? [laravel]

时间:2020-06-13 09:51:05

标签: laravel eloquent laravel-7

我有Team有口才,有TeamObserverTeamObserverdeleting event,如果我打电话给TeamDeletingEventTeamDeletingEvent分派TeamDeletingListener。如果TeamDeletingLister返回false会停止团队删除功能吗?

TeamObserver

class TeamObserver
{
    /**
     * Handle the team "deleting" event.
     *
     * @param  Team  $team
     * @return void
     */
    public function deleting(Team $team)
    {
        event(new TeamDeletingEvent($team));
    }
}

TeamDeletingEvent

class TeamDeletingEvent
{
    use SerializesModels;

    /**
     * @var Team
     */
    public $team;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Team $team)
    {
        $this->team = $team;
    }
}

TeamDeletingListener

class TeamDeletingListener
{
    /**
     * Handle the event.
     *
     * @param  TeamDeletingEvent  $event
     * @return bool
     */
    public function handle(TeamDeletingEvent $event)
    {
        $teamUser = Team::where('id', $event->team->id)->users()->first();
        if(is_null($teamUser)){
           return true;
        }
        return false;
    }
}

除了TeamObserverAppServiceProvider Event and Listener中注册的EventServiceProvider

1 个答案:

答案 0 :(得分:1)

侦听器返回false不会停止删除功能。它将停止事件的连续侦听器调用。但是,雄辩的事件返回false将停止删除功能。 (模型实例值已更新,但数据库中未更新)。更多信息Detail Answer