@charrua I have a way to approach this problem, which uses 3 relatively fast queries on the database and retrieves the courses without employing a relationship method.
Please let me know if this helps you out. If you use the code listed below, you should be able to fetch all courses for a user by calling $user->courses().
Models
class Course extends Model
{
public function enrollments()
{
return $this->belongsToMany(Enrollment::class);
}
}
class Enrollment extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function courses()
{
return $this->belongsToMany(Course::class);
}
}
class User extends Authenticatable
{
// default methods omitted for clarity...
public function enrollments()
{
return $this->hasMany(Enrollment::class)->with('courses');
}
public function courses()
{
return collect($this->enrollments)->flatMap(function ($enrollment) {
return $enrollment->courses;
});
}
}
Migrations
The users table migration is default.
// For the 'enrollments' table
public function up()
{
Schema::create('enrollments', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id');
$table->timestamps();
});
}
// For the 'courses' table
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
// For the course_enrollment pivot table
public function up()
{
Schema::create('course_enrollment', function (Blueprint $table) {
$table->foreignId('course_id');
$table->foreignId('enrollment_id');
$table->unique(['course_id', 'enrollment_id']);
});
}