Reputation: 552
I have the following relationships
- a movie has many episodes
- an user has a watchlist
- a watchlist has many movies
I get my episodes with eloquent
$latestshows = episode::with('movies') ->where('category', 'tvshow')->take(10) ->get();
I then display it with
@foreach($latestshows as $show)
@if (Auth::guest())
<tr>
@else
<tr class="{{ Auth::user()-> watchlist **...** ? 'alert-danger' : '' }}">
@endif
<td><a href="/movies/{{$show->movie->id}}">{{ $show->movie->title }}</a></td>
<td>{{ $show->number }}</td>
<td>{{ $show->created_at }}</td>
</tr>
@endforeach
How do I check if the logged in used has the show in watchlist? I want to display it with a different color in that case. Sorry if this is a silly question, I'm just a beginner and experimenting.
Upvotes: 0
Views: 430
Reputation: 633
You can use in_array or array_diff (Both native PHP functions) to see which movies are the same, or you can use the ->diff() or ->has() methods from the Collection objects that you are dealing with.
<td>
<a ... {{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}>
{{ $show->movie->title }}
</a>
</td>
Upvotes: 1
Reputation: 552
This code gave me the expected result
{{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}
Thank you Oscar, you were my inspiration :P
Upvotes: 0