Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AdeMike's avatar

How to check those that watched a video?

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.

I don't know how to go about this?

0 likes
3 replies
martinbean's avatar

@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.

aschmelyun's avatar

@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:

$hasWatchedVideo = DB::table('completed_course')->where('user_name', $user->name)->where('episode', $episode)->first();
marlie's avatar

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');
	}

}

Laravel's documentation for Eloquent HasManythrough relationships can be found (here)[https://laravel.com/docs/7.x/eloquent-relationships#has-many-through]

Please or to participate in this conversation.