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

ethar's avatar
Level 5

get all courses related to student

i work on website like udemy, i have these tables students (id, name) courses (id, name)

and pivot table

            $table->foreignId('student_id')->constrained()->cascadeOnDelete();
            $table->foreignId('course_id')->constrained()->cascadeOnDelete();
    });

in route

        Route::controller(DashboardController::class)->group(function () {
            Route::group(['middleware' => ['auth:students']], function () {
                Route::get('/student_courses', 'courses')->name('student.courses');
            });
        });

in student model

class Student extends Authenticatable
{
    public function courses()
    {
        return $this->belongsToMany(Course::class, 'course_students');
    }
}

i want to when user logedin dashboard and go to student_courses, to get all courses.

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve the functionality where a logged-in student can view all their courses, you need to set up a method in your DashboardController that retrieves the courses for the currently authenticated student. Here's how you can do it:

First, ensure that your Student model has the correct relationship method to retrieve the associated courses. It looks like you've already defined the courses method, which is great. Make sure your Course model also has the inverse relationship defined.

Now, in your DashboardController, you would have a method like this:

use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function courses()
    {
        // Get the currently authenticated student
        $student = Auth::guard('students')->user();

        // Retrieve all courses related to the student
        $courses = $student->courses;

        // Return the courses, you might want to return a view or JSON response
        return view('student.courses', compact('courses'));
    }
}

Make sure you have a view file at resources/views/student/courses.blade.php where you can display the courses to the student.

Here's an example of what the view might look like:

@extends('layouts.app')

@section('content')
    <h1>Your Courses</h1>
    <ul>
        @foreach ($courses as $course)
            <li>{{ $course->name }}</li>
        @endforeach
    </ul>
@endsection

This is a basic example, and you might need to adjust it based on your specific requirements and the structure of your application. If you're returning a JSON response instead of a view, you can replace the return view(...) line with return response()->json($courses);.

Remember to import the necessary classes at the top of your DashboardController if they are not already imported.

1 like
kiwi0134's avatar

What are you currently trying and what issues do you face? Please post the corresponding code.

Also please note that you can chain most router methods, so you don't have to nest them so much.

Please or to participate in this conversation.