Laravel 4 Laravel雄辩的belongtomany()->;getResults()忽略软删除
使用Laravel4.2.*,我有一个实现软删除的透视表。当我运行Model::all()时,我得到了预期的结果,但是当我通过父级访问数据时,我也得到了删除的行 父模型分类具有以下代码:Laravel 4 Laravel雄辩的belongtomany()->;getResults()忽略软删除,laravel-4,Laravel 4,使用Laravel4.2.*,我有一个实现软删除的透视表。当我运行Model::all()时,我得到了预期的结果,但是当我通过父级访问数据时,我也得到了删除的行 父模型分类具有以下代码: public function organizations(){ return $this->belongsToMany('Organization', 'organization_classifications', 'classification_id', 'organization_id')
public function organizations(){
return $this->belongsToMany('Organization', 'organization_classifications', 'classification_id', 'organization_id')->getResults();
}
透视模型OrganizationClassification如下所示:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class OrganizationClassification extends BaseCrudModel{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $table = 'organization_classifications';
}
该表有一个可为空的列,已在处删除,两条记录中的一条在该列中具有正确的datetime值
如果执行OrganizationClassification::all(),则返回一行,但如果执行以下操作:
$c = Classification::find(1);
$ret = $c->rganizations();
我退后两排
是否有某种方法可以仅返回未删除的行,但以getResults提供的混合格式返回?由于软删除列位于数据透视表上,因此您应该尝试为此设置一个条件:
public function organizations()
{
return $this->belongsToMany('Organization', 'organization_classifications', 'classification_id', 'organization_id')->whereNull('organization_classifications.deleted_at');
}
这将排除
deleted_at
为null
的条目,我猜BaseCrudModel
扩展了Eloquent
。另外,getResults()
做什么,因为它不是查询生成器API的一部分?API文档除了“获取关系的结果”之外,没有太多说明getResults()返回什么。它是一个混合数组,并提取行、轴,可能(我不记得)关联行。是的,BaseCrudModel扩展得很有说服力。