Well if the logged in user is a student just do auth()->user()->courses()->get() instead of Auth::guard('student');
Sep 4, 2021
11
Level 1
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.
```
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
Please or to participate in this conversation.