2

In Laravel, is it possible to put a relationship within a relationship?

Basically, I already have a relationship set up, but I then need another relationship for that relationship (if that makes sense).

My two models:

  • Store
  • Supplier

My relationship model for Store and Supplier:

  • SupplierStore
  • store_id
  • supplier_id

As you can see, I have many stores and suppliers, each of whom can relate to each other through the SupplierStore table.

This works perfectly.

My problem is that for each relationship (of Store and Supplier) there can be many Delivery Days.

My HasMany relationship:

DeliveryDay
supplier_store_id
day

I thought if I go into the SupplierStore model and add:

public function days()
{
 return $this->hasMany('App\DeliveryDay');
}

Would work, but there is no way I can seem to access this data through eloquent?

I was hoping to do something like this:

$supplier = Supplier::find($id); $supplier->store->pivot->days

The pivot (SupplierStore) would hold the relationship of that specific relation and I could grab the days.

Can anyone help me figure out what I need to do to achieve this? Your time is much appreciated. After googling for hours, my head hurts so much. :)

Mr Digital
  • 29
  • 3
  • is SupplierStore already a model? – dparoli Mar 06 '19 at 09:14
  • hasManyThrough? - https://laravel.com/docs/5.8/eloquent-relationships#has-many-through – CodeBoyCode Mar 06 '19 at 09:30
  • Probably a duplicate of https://stackoverflow.com/questions/17367440/using-pivot-model-data-as-a-relationship-to-another-model-in-laravel-4s-eloquen – Bharat Geleda Mar 06 '19 at 09:31
  • Yeah, SupplierStore is a model. It's the model which is used for the BelongsToMany relationship between Supplier and Store. That model itself also has a relationship of HasMany for DeliveryDays. – Mr Digital Mar 06 '19 at 10:07
  • I'm having the same problem. I need to define a relationship inside the pivot model for making easier queries. How did you solve it? – Zalo Dec 15 '19 at 20:00

1 Answers1

0

You could access each day like this:

public function getDays($id)
{
 $supplier = Supllier::find($id);

 foreach($supplier->stores() as $store)
 {
   foreach($store->days() as $day)
   {
    echo $day->day;
   }
 }
}

Or get all days

$supplier->stores->days;
Piazzi
  • 2,490
  • 3
  • 11
  • 25
  • Hey Lucas, I appreciate your help. But I don't think what you've suggested is the solution. What I want to access the relation within the relation. Eg $supplier->store->pivot->days – Mr Digital Mar 06 '19 at 21:38
  • Oh my bad, so you want to acess the days using your supplier model? – Piazzi Mar 06 '19 at 21:46
  • Yeah man, it seems like it's just not possible. Which sucks! I if I dd($supplier->stores->pivot) - there is a relations line but it is a blank array, it doesn't have the days() relation I added into the relation. Really weird. – Mr Digital Mar 06 '19 at 23:29
  • ```$supplier->stores``` could you dd this and see if is returning your SupplierModel? if does you can acess the days using a foreach loop – Piazzi Mar 06 '19 at 23:42
  • So if I do something like this. $supplier->stores->first(), it will show me the first relation. I can then do $supplier->stores->first()->pivot which will show me the actual relationship model. Inside the "pivot" I can't see days, even if I try and foreach say $supplier->stores->pivot->days it thinks it is a column on my SupplierStore model not a relationship. If I do $supplier->stores->pivot->days() it just says function doesn't exist. – Mr Digital Mar 06 '19 at 23:52