我有4张桌子,
道具,上市,优惠,联系
道具有很多上市,上市属于道具
public function listings(){return $ this-> hasMany('App \ Models \ Listing \ Listing'); }
优惠属于上市,
public function property()
{
return $this->belongsTo('App\Models\Property\Property')->with('owners');
}
然后提供belongsToMany联系槽offer_contact表
public function buyers()
{
return $this->belongsToMany(Contact::class, 'offer_contact', 'offer_id', 'contact_id')->with('primary_email');
}
我的问题是,如何访问买家()?
像$ props-> buyer()之类的东西
在道具模型中,我所做的是
return $this->hasManyThrough('App\Models\Offer\Offer', 'App\Models\Listing\Listing');
你不能。您可以使用嵌套迭代来获取属性,列表属于每个属性,商品属于每个列表,然后是属于该商品的客户。
或者,您可以使用原始查询来使用DB::statement();
获得所需的结果
我创造了一个无限级别的HasManyThrough
关系:Repository on GitHub
安装完成后,您可以像这样使用它:
class Property extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function buyers() {
return $this->hasManyDeep(Contact::class, [Listing::class, Offer::class, 'offer_contact']);
}
}