Laravel 4删除多个与外键相关的模型 [英] Laravel 4 deleting multiple models related by foreign keys

查看:454
本文介绍了Laravel 4删除多个与外键相关的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有三个模型,当他们之中的一个 dAgency 被删除的时候 delete - ()像所有三个被删除。问题是他们中的两个被删除,而顶级父母 DemDataSet 没有被删除。另外,当我打电话时:

echo< pre>,dd(dAgency :: find(21) - > DemographicReport() - > DemDataSet() - > get()),< / pre>;



code>调用undefined方法Illuminate \Database\Query\Builder :: DemDataSet()但是当我尝试时:

echo< pre>,dd(dAgency :: find(21) - > DemographicReport() - > get()),< / pre>;



有用。所以我知道问题是我的DemDataSet模型之间的关系。下面是我的模型:

 <?php 

类DemDataSet扩展了雄辩{b
$ b public $ timestamps = false;
保护$连接='epcr_dem_data';
保护$ table ='DEMDataSet';
protected $ primaryKey ='pk_DEMDataSet';
$ b $ public function DemographicReport(){
return $ this-> hasOne('DemographicReport','fk_DEMDataSet','pk_DEMDataSet');
}


}

class DemographicReport extends Eloquent {

public $ timestamps = false;
保护$连接='epcr_dem_data';
保护$ table ='DemographicReport';
保护$ primaryKey ='pk_DemographicReport';
$ b $ public function DemDataSet(){
return $ this-> belongsTo('DemDataSet','fk_DEMDataSet','pk_DEMDataSet');


public function dAgency(){
return $ this-> hasOne('dAgency','fk_DemographicReport','pk_DemographicReport');


public function delete(){
parent :: delete();
return $ this-> DemDataSet() - > delete();
}

}

类dAgency扩展雄辩{

public $ timestamps = false;
保护$连接='epcr_dem_data';
保护$ table ='dAgency';
保护$ primaryKey ='pk_dAgency';

public function DemographicReport(){
return $ this-> belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport');


public function dAgency_10(){
return $ this-> hasMany('dAgency_10','fk_dAgency','pk_dAgency');


public function delete(){
parent :: delete();
return $ this-> DemographicReport-> delete();
}

}

?>

现在我已经摔跤了两天了!我真的很感激你花时间看这个。

解决方案

正确的方式来查询laravel模型的关系是这样的:

  //这是从DemographicReport 
中提取所有DemDataSet dAgency :: find(21) - > DemographicReport ) - >首先() - > DemDataSet;

//如果你需要进一步查询,那么你调用DemDataSet作为一个方法
dAgency :: find(21) - > DemographicReport() - > first() - > DemDataSet () - > where('your condition here') - > get();


So I have three models and when when one of them dAgency is deleted delete-() I'd like all three to be deleted. The problem is that two of them are being deleted, while the top parent one DemDataSet isn't being deleted. Additionally, when I call:

echo "<pre>", dd(dAgency::find(21)->DemographicReport()->DemDataSet()->get()), "</pre>";

I get this error: Call to undefined method Illuminate\Database\Query\Builder::DemDataSet() But when I try:

echo "<pre>", dd(dAgency::find(21)->DemographicReport()->get()), "</pre>";

It works. So I know the problem is my relation between my DemDataSet model. Below is my model:

<?php

class DemDataSet extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DEMDataSet';
    protected $primaryKey = 'pk_DEMDataSet';

    public function DemographicReport(){
        return $this->hasOne('DemographicReport','fk_DEMDataSet','pk_DEMDataSet');
    }


}

class DemographicReport extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DemographicReport';
    protected $primaryKey = 'pk_DemographicReport';

    public function DemDataSet (){
        return $this->belongsTo('DemDataSet','fk_DEMDataSet','pk_DEMDataSet');
    }

    public function dAgency(){
        return $this->hasOne('dAgency','fk_DemographicReport','pk_DemographicReport');
    }

    public function delete(){
        parent::delete();
        return $this->DemDataSet()->delete();
    }

}

class dAgency extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'dAgency';
    protected $primaryKey = 'pk_dAgency';

    public function DemographicReport(){
        return $this->belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport');
    }

    public function dAgency_10(){
        return $this->hasMany('dAgency_10','fk_dAgency','pk_dAgency');
    }

    public function delete(){
        parent::delete();
        return $this->DemographicReport->delete();
    }

}

?>

I've been wrestling with this one for two days now! I really appreciate you taking the time to look at this.

解决方案

The right way to query laravel model with relationship is this way:

//This is to pull all DemDataSet from DemographicReport
dAgency::find(21)->DemographicReport()->first()->DemDataSet;

//If you need further query, then you call DemDataSet as a method
dAgency::find(21)->DemographicReport()->first()->DemDataSet()->where('your condition here')->get();

这篇关于Laravel 4删除多个与外键相关的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆