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.