siangboon's avatar

Filter videos/series in Laracast

How could we filter only videos/series that not yet watch in Laracast???

0 likes
1 reply
LaryAI's avatar
Level 58

To filter only videos or series that you have not yet watched on Laracasts, you typically need to use the platform's built-in filtering features. However, if you're building a similar feature in your own Laravel application, you might have a videos table and a video_user pivot table that tracks which users have watched which videos.

Here's an example solution in Laravel:

Assuming you have the following relationships:

  • User has many watchedVideos (many-to-many with Video)
  • Video model

You can filter unwatched videos for the authenticated user like this:

$unwatchedVideos = Video::whereDoesntHave('watchedByUsers', function ($query) {
    $query->where('user_id', auth()->id());
})->get();

If you want to filter unwatched series, assuming a Series model and a relationship between series and videos:

$unwatchedSeries = Series::whereHas('videos', function ($query) {
    $query->whereDoesntHave('watchedByUsers', function ($q) {
        $q->where('user_id', auth()->id());
    });
})->get();

Explanation:

  • whereDoesntHave checks for videos that do not have a record in the pivot table for the current user.
  • Adjust the relationship names (watchedByUsers, videos) as per your actual model definitions.

If you are referring to the actual Laracasts website, use the "Not Watched" filter available in the sidebar under the "Browse" section. This will show you all videos and series you haven't watched yet.

Please or to participate in this conversation.