Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afujita's avatar

Many to many, 3 tables

I have 3 tables for my site (laravel 4.2)

  1. events
  • id
  • name
  1. bands
  • id
  • name
  1. artists
  • id
  • name

and 1 pivot table event_band_artist

| event_id  | band_id | artist_id |
| --------- | ------- | --------- |
|    1      |    1    |     1     |
|    1      |    1    |      2    |
|    1      |    2    |      3    |

Event Model

public function bands() {
return $this->belongsToMany('Band', 'event_band_artist');
}

public function artists() {
return $this->belongsToMany('Artist', 'event_band_artist');
}

Band Model

public function events() {
return $this->belongsToMany('Event', 'event_band_artist');
}
public function artists() {
return $this->belongsToMany('Artist', 'event_band_artist');
}

Artist Model

public function events() {
return $this->belongsToMany('Event', 'event_band_artist');
}

public function bands() {
return $this->belongsToMany('Band, 'event_band_artist');
}

Controller

$events = Event::find($id);
return View::make('event')->with('events', $events);

View

<h3>{{ $events->name }}</h3>
<ul>
@foreach ($events->bands as $band)
<li>{{ $band->name }}</li>
</ul>
@endforeach

Which results

Event I

  • Band A
  • Band A
  • Band B

My problem here is that foreach is retrieving Band A (band_id = 1) twice, because the pivot table contains 2 records of the same Band for the Event 1. But in this case, I am repeating the Band 2x because it has 2 different artists, as you can in the table at the top of this post. That is, an artist can belongs to more than one Band, depending on the Event.

So what I want to know is how to retrieve Bands filtered by the event_id, so that the same Band will not load more than one time in the View page.

Or should I think at another kind of relationship among these 3 tables?

Thanks for your time.

Alex

0 likes
4 replies
ARCANEDEV's avatar

If you get a pivot table with more than two FKs, try to treat it as full model.

This also helps you think about the relationship when trying to name that model in a way that makes sense (For example : instead of event_band_artist table, rename it as programs).

Hope this helps.

6 likes
afujita's avatar

Thanks for your help, Arcanedev! Yeah, programs would be a better choice to name the relationship. Can you explain me how to treat this pivot as a full model? And how would be the relationships among the tables?

ARCANEDEV's avatar

Really simple, instead of Many-to-Many relationship. You divide it into multiple of One-to-Many relationships.

In your case, you're going to have 4 models : Event, Artist, Band, Program.

  • Event hasMany Program.
  • Artist hasMany Program.
  • Band hasMany Program.

-------------

  • Program belongsTo Event.
  • Program belongsTo Artist.
  • Program belongsTo Band.

For more details, check the Laravel documentation about the One-to-Many relationships : https://laravel.com/docs/5.2/eloquent-relationships#one-to-many

7 likes
afujita's avatar

Thanks again! I'll try here and let you know.

Please or to participate in this conversation.