首页 > 解决方案 > Access Method in a hasManyThorugh

问题描述

I have 4 tables,

props, listing, offers, contact

props has many listing, listing belongs to props

public function listings() { return $this->hasMany('App\Models\Listing\Listing'); }

offer belongs to listing,

public function property()
{
    return $this->belongsTo('App\Models\Property\Property')->with('owners');
}

then offer belongsToMany contact trough offer_contact table

public function buyers()
{
    return $this->belongsToMany(Contact::class, 'offer_contact', 'offer_id', 'contact_id')->with('primary_email');
}

My question is, how to access buyers()?

Something like $props->buyers()

In props model, what I did is

return $this->hasManyThrough('App\Models\Offer\Offer', 'App\Models\Listing\Listing');

标签: laraveleloquent

解决方案


你不能。您可以使用嵌套迭代来获取属性,列表属于每个属性,优惠属于每个列表,然后是属于该优惠的客户。

或者,您可以使用原始查询来获得所需的结果DB::statement();


推荐阅读