0

I have three tables that are related through other tables :

Serie

 public function episodes() 
    {
        return $this->hasManyThrough(Episode::class, Season::class);
    }

Episodes

public function chaines() 
    {
        return $this->belongsToMany(Channel::class, 'programmation', 'episode_id', 'channel_id');
    }

Channel

public function episodes() 
{
    return $this->belongsToMany(Episode::class, 'programmation', 'channel_id', 'episode_id');
}

I would like to have channels for one serie. I need to check for the programmation table. In my blade view, I have this :

 @foreach($serie->episodes as $episode) 
@foreach($episode->chaines as $chaine){{$chaine->name}}
@endforeach 
@endforeach 

But it gives me the channel of all the episodes (the same one essentially). How can I do to have just one channel if they're the same ? Sometimes, the episodes are on different channel so i need to check that too.

Charlotte
  • 25
  • 5
  • i'm sorry, i've not fully understood what you want. You have series, every serie has some episodes? what i can't figure out is the relations between the three models, can you please try to explain again? – Alberto Sinigaglia Apr 29 '20 at 14:57
  • If i understood correctly you are looking for Eloquent Distinct. https://stackoverflow.com/questions/28651727/laravel-eloquent-distinct-and-count-not-working-properly-together – Collin Apr 29 '20 at 14:59
  • Every serie has multiple episodes. Each episode has a programmation with a channel. I would like the channel for one given serie – Charlotte Apr 29 '20 at 15:12
  • If any given series has many episodes and each episode can belong to many channels, and the only relation between series and channels comes from episodes, how do you expect to get just one channel for that series? – kidA Apr 29 '20 at 15:21
  • Yes of course, i understand but if there is multiple channels, it's ok. But if every episode of a serie has the same channel, i don't want to have this channel multiple times on my view – Charlotte Apr 29 '20 at 15:27

0 Answers0