使用 Laravel 的 Eloquent 删除 hasManyThrough 关系行

Delete hasManyThrough relationship rows using Laravel's Eloquent

我有三个模型,AdvertiserPtcAdPtcCampaign。删除 Advertiser 时,我想删除所有相关的 PtcAdsPtcCampaignsAdvertiser 通过 PtcAds.

有许多 PtcCampaigns

广告客户模型

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();
}

Ptc活动模型

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));
}

test_delete_advertiser()测试中的$this->assertFalse(PtcCampaign::all()->contains($ptcCampaign))失败,为什么?

我有更多的测试来确保所有的关系都有效,所以我真的不知道什么可能是错的。我的下一次尝试是在 Advertiser 的 delete() 方法中创建 foreach,但也许有更简单的方法,我想了解为什么这不起作用。

您可以使用 Laravel 的模型事件 (deleting) 删除相关模型,如下所示:

class Advertiser extends Eloquent
{
    public function ptcAds()
    {
        return $this->hasMany('PtcAd');
    }

    // this is a recommended way to declare event handlers
    protected static function boot() {
        parent::boot();

        static::deleting(function($adv) { // before delete() method call this
             $adv->ptcAds()->delete();
             // do the rest of the cleanup...
        });
    }
}

// Same for PtcCompaigns

class PtcAd extends Eloquent
{
    public function ptcCompaigns()
    {
        return $this->hasMany('PtcCompaigns');
    }

    // this is a recommended way to declare event handlers
    protected static function boot() {
        parent::boot();

        static::deleting(function($ptc_ad) { // before delete() method call this
             $ptc_ad->ptcCompaigns()->delete();
             // do the rest of the cleanup...
        });
    }
}

希望对您有所帮助!

看来问题出在删除语句的顺序上。

尝试按如下所示更改顺序:

public function delete()
{
    $this->ptcCampaigns()->delete();

    $this->ptcAds()->delete();

    return parent::delete();
}