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

DInu98's avatar

How to query authenticated Student's Enrolled Courses

i need to get student who logged into their account and he can see what are the courses he enrolled.

 public function courses()
    {
        return $this->belongsToMany(Course::class,'course_student');
    }
public function students()
    {
        return $this->belongsToMany(Student::class,'course_student');
    }
 $course = Auth::guard('student')->courses()->get();
        return view('dashboard.user.myclass',[
            'course'=>$course
        ]);
@section('content')
<div class="container">
   @foreach ($course as $crs )
       {{ $crs->Name }}
   @endforeach
</div>
@endsection

i tried this method but there is a error message showing like this

```
Error!
Method Illuminate\Auth\SessionGuard::courses does not exist.
```
0 likes
11 replies
teos_97's avatar

Well if the logged in user is a student just do auth()->user()->courses()->get() instead of Auth::guard('student');

1 like
DInu98's avatar

hi, Thanks for Helping me.. i tried that method you mentioned. but try to get results like this.. unfortunately it doesn't show any results

public function index()
    {
       $course = auth()->user()->courses()->get();
        return view('dashboard.user.myclass',[
            'course'=>$course
        ]);
    }
}
<div class="container">
   @foreach ($course as $crs )
       {{ $crs->id }}
	   {{ $crs->Name}}
   @endforeach
</div>
MichalOravec's avatar

Do you have student guard?

$course = Auth::guard('student')->user()->courses()->get();
1 like
DInu98's avatar

yeah i have student guard

  'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        
        'admin'=>[
            'driver'=>'session',
            'provider'=>'admins',
        ],

        'student'=>[
            'driver'=>'session',
            'provider'=>'students',
        ],

         
       
    ],
jlrdw's avatar

Just curious, why all the different guards? You still authorization.

1 like
DInu98's avatar

i need to authenticate different users like -> students/admins/Teachers

DInu98's avatar

no it works. error is solved now but i didn't get any results..

public function index()
    {
        $courses = Auth::guard('student')->user()->courses()->get();
        return view('dashboard.user.myclass',[
            'courses'=>$courses
        ]);
    }
<div class="container">
   @foreach ($courses as $course )
       <h1>{{ $course->id}}</h1>
   @endforeach
</div>
MichalOravec's avatar
Level 75

You don't have data in the database.

public function index()
{
    $courses = Auth::guard('student')->user()->courses()->get();
    
    return view('dashboard.user.myclass', compact('courses'));
}
<div class="container">
    @forelse ($courses as $course)
        <h1>{{ $course->id }}</h1>
    @empty
        <p>No data</p>
    @endforelse
</div>
1 like
DInu98's avatar

thanks you very much... yeah i don't have data in my database....

teos_97's avatar

Can you show us the table structure ? I suspect it has to do with the way relationships have been setup.

1 like

Please or to participate in this conversation.