Does Eloquent have something like "whenHas"?
I'm having a bit of trouble coming up with a query to get the information I need. For the sake of an example, let's say we have a movie site where users can "dislike" movies. So we'd have 3 tables:
MOVIES
id
title
USERS
id
name
MOVIE_USER
id
movie_id
user_id
The "MOVIE_USER" table there stores the ID of the movie the user has disliked as well as the user's ID.
Now, let's say on the homepage we want to display all the movies on the site, EXCEPT for the movies a user has disliked. So in theory:
Movie::whereHas('dislikes', function ($q) use ($user)
{
$q->where('movie_user.user_id', '!=', $user->id);
});
The problem with this is, it will only grab movies that exist in the "movie_user" table, and not ones that haven't been disliked by anyone. Ideally I'd like to run the sub query only WHEN the movie has dislikes, so it would still grab the rest of the movie list.
Please or to participate in this conversation.