数据透视表中的 Laravel 观察器未触发

问题描述 投票:0回答:1

我为我的数据透视表创建了一个观察者

CreatorListing
,我不知道为什么它不会触发
created
事件。我什至尝试过像
creating, saving, and saved
这样的其他人,但他们不会通过。

这是在我的控制器里

    public function getSponsored(Listing $listing)
    {
        $this->authorize('canRequestGetSponsored', $listing);
        $listing->creators()->attach(auth()->user()->creator->id, [
            'workflow' => new ValueObjectsWorkflow(PlatformType::Google)
        ]);

        return response()->ok(null, __('Listing added. Accept the contract to continue.'));
    }

在我的列表模型中

   public function creators()
    {
        return $this->belongsToMany(Creator::class)
            ->withPivot('counter_offer', 'workflow', 'is_contract_signed', 'is_accepted', 'review_status')
            ->withTimestamps()->using(CreatorListing::class);
    }

在我的观察者中,它只是一个简单的

dd
声明

    public function created(CreatorListing $creatorListing)
    {
        dd(1);
    }

之前,我什么时候没有添加

->using(CreatorListing::class);
行。代码没有显示错误,但它不会调用
dd
或观察者事件。现在我添加了那行,它说
Array to string conversion
。我不知道我错过了什么或者我做错了什么?

我只是按照这篇文章做参考https://mayahi.net/laravel/observe-pivot-tables-in-laravel/

php mysql laravel eloquent observers
1个回答
0
投票
public function boot()
{
    Post::observe(PostObserver::class);
}

在服务提供商中添加。

© www.soinside.com 2019 - 2024. All rights reserved.