0
votes

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');
2

2 Answers

0
votes

You cannot. You may use nested iterations to get properties, listings belongs to each property, offers belongs to each listing and then customers belonging with the offer.

Alternatively, you may use the raw query to get the desired result using DB::statement();

0
votes

I created a HasManyThrough relationship with unlimited levels: Repository on GitHub

After the installation, you can use it like this:

class Property extends Model {
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function buyers() {
        return $this->hasManyDeep(Contact::class, [Listing::class, Offer::class, 'offer_contact']);
    }
}