我为我的数据透视表创建了一个观察者
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/
public function boot()
{
Post::observe(PostObserver::class);
}
在服务提供商中添加。