我有三個模型,Advertiser
,PtcAd
和PtcCampaign
。當刪除廣告商我想要刪除所有相關的PtcAds和PtcCampaigns。該廣告有很多PtcCampaigns通過PtcAds。使用Laravel的Eloquent刪除hasManyThrough relatioship行
廣告模式
use SoftDeletes;
protected $dates = ['deleted_at'];
public function ptcAds()
{
return $this->hasMany('App\PtcAd');
}
public function ptcCampaigns()
{
return $this->hasManyThrough('App\PtcCampaign', 'App\PtcAd');
}
public function delete()
{
$this->ptcAds()->delete();
// I'VE TRIED WITH AND WITHOUT THIS
$this->ptcCampaigns()->delete();
return parent::delete();
}
PtcAd型號
use SoftDeletes;
protected $fillable = ['advertiser_id', 'title'];
protected $dates = ['deleted_at'];
public function advertiser()
{
return $this->belongsTo('App\Advertiser');
}
public function ptcCampaigns()
{
return $this->hasMany('App\ptcCampaign');
}
public function delete()
{
$this->ptcCampaigns()->delete();
return parent::delete();
}
PtcCampaign型號
use SoftDeletes;
public $timestamps = false;
protected $fillable = ['ptc_ad_id', 'clicks'];
protected $dates = ['paused_at', 'deleted_at'];
public function ptcAd()
{
return $this->belongsTo('App\PtcAd');
}
個我的測試:
public function test_delete_advertiser()
{
$advertiser = factory(Advertiser::class)->create();
$ptcAd = factory(PtcAd::class)->create(['advertiser_id' => $advertiser->id]);
$ptcCampaign = factory(PtcCampaign::class)->create(['ptc_ad_id' => $ptcAd->id]);
$this->assertTrue($advertiser->delete());
$this->assertFalse(Advertiser::all()->contains($advertiser));
$this->assertFalse(PtcAd::all()->contains($ptcAd));
// THE FOLLOWING TEST DOESN'T WORK!
$this->assertFalse(PtcCampaign::all()->contains($ptcCampaign));
}
// ALL OF THE FOLLOWING TESTS WORK!
public function test_delete_ad()
{
$ptcAd = factory(PtcAd::class)->create();
$ptcCampaign = factory(PtcCampaign::class)->create(['ptc_ad_id' => $ptcAd->id]);
$this->assertTrue($ptcAd->delete());
$this->assertFalse(PtcAd::all()->contains($ptcAd));
$this->assertFalse(PtcCampaign::all()->contains($ptcCampaign));
}
的$this->assertFalse(PtcCampaign::all()->contains($ptcCampaign))
在test_delete_advertiser()
測試失敗,爲什麼?
我有更多的測試,以確保所有關係工作,所以我真的不知道什麼可能是錯誤的。我的下一個嘗試是在廣告客戶的delete()方法中製作foreach
,但也許有一些更簡單的方法,我想了解爲什麼這種方法無效。
這就行了!謝謝!現在我有另一個相關的問題,因爲'PtcAd'模型具有設置爲刪除關係中的'PtcCampaign'的'delete()'方法,爲什麼我需要調用'$ this-> ptcCampaigns() - > delete ();'從'Advertiser'模型?它不應該鏈嗎? – DanVeira
當你執行'$ this-> ptcAds() - > delete()'時,它會創建查詢並刪除行,以便調用delete函數查詢構建器類。但是,如果您使用'foreach'刪除'ptcAds',那麼它會調用Model類的刪除函數,並且還會刪除'ptcCampaigns'。 –