I have this two tables, in my app, i host my videos on vimeo. If a user finish watching an episode of a course, I saved the user details into a particular table with the particular episode they watched. However, I want to let the user to know they have watched this episode when they come back next time.
since, a user is not attached to a video on the same table, the only way to know is when their name is saved on this completed_course table.
@ademike Just create a pivot table between users and videos. If there’s a row, then that user had watched that video. Therefore when viewing the video, you can just query this table: if there’s a row, the user has already watched the video.
@ademike It sounds like you're already storing the user's name and (I'm assuming) video episode on this completed_course table, you can query it when returning a view for the video and use it to determine if that user has watched it or not.
Something like this:
$hasWatchedVideo = CompletedCourse::where('user_name', $user->name)->where('episode', $episode)->first();
if ($hasWatchedVideo) {
// user has watched the video.
// this variable will be either null or an Eloquent model object
}
Alternatively if you're not using a class for the table:
Assuming you already have a pivot table called completed_course and a model for it CompletedCourse, you can move the logic into the Users model so that in your controller you're just calling $user->hasCompleted($video), which is very readable. The user model logic would look like this:
class User {
/**
* Return whether or not the user has completed the video
*/
public function hasCompleted(Video $video) {
return $this->completedVideos()->where('video_id', $video->id)->exists();
}
/**
* Eloquent relationship to query completed videos through completed_course table.
*/
public function completedVideos() {
return $this->hasManyThrough('App\Video', 'App\CompletedCourse');
}
}