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

mehrdad70's avatar

many to many relationship

Hi everyone I have a table called the course and I also have a user table Now users who buy the course user_id and course_id are stored in the course_user table The relationship between the course and the user is many to many Now what query can I make so that the user's purchased courses are logged in and visible in the user panel?

        Schema::create('course_user', function (Blueprint $table) {

            $table->foreignId('user_id');
            $table->foreignId('course_id');

            $table->timestamps();
            $table->primary(['user_id' , 'course_id']);

            $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('CASCADE');

        });

Course.php

    public function students()
    {
        return $this->belongsToMany(User::class , 'course_user' , 'course_id' , 'user_id');
    }

User.php

    public function purchases()
    {
        return $this->belongsToMany(Course::class , 'course_user' , 'user_id' , 'course_id');
    }
    public function myCourses()
    {
        $courses = Course::with('students')->get();
        $courses->students->contains(auth()->id());

        return view('front.users.my-courses', ['courses' => $courses]);
    }
                            @foreach ($courses as $course)
                            <tr>										
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="flex-shrink-0 mr-20">
                                            <div class="bg-img h-50 w-50" style="background-image: url(../images/front-end-img/courses/1.jpg)"></div>
                                        </div>

                                        <div>
                                            <a href="#" class="text-dark font-weight-600 hover-primary mb-1 font-size-16">{{$course->title}}</a>
                                            <span class="text-fade d-block">{{$course->price}}</span>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    Programming
                                </td>
                                <td>
                                    k
                                </td>
                                <td>
                                    Sophia Pharetra
                                </td>
                                <td>
                                    <span class="badge badge-success badge-lg">Approved</span>
                                </td>
                                <td>
                                    <div class="d-flex justify-content-end gap-items-1">
                                        <a href="#" class="waves-effect waves-light btn btn-primary btn-xs btn-circle"><span class="icon-Bookmark"></span></a>
                                        <a href="#" class="waves-effect waves-light btn btn-primary btn-xs btn-circle"><span class="icon-Arrow-right"><span class="path1"></span><span class="path2"></span></span></a>
                                    </div>
                                </td>
                            </tr>
                            @endforeach
0 likes
2 replies
martinbean's avatar
Level 80

@mehrdad70 You’re not querying your many-to-many relationship properly; you’re going completely the wrong way around it.

Your user model has a purchases relationship. That is the courses the user has purchased, so just query that:

public function myCourses(Request $request)
{
    $courses = $request->user()->purchases()->get();

    return view('front.users.my-courses', compact('courses'));
}

Please or to participate in this conversation.