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

ramniksingh's avatar

Access related tables with hasManyThrough non-linear relations

I have 4 tables / models

1 . User -- Linked to CourseUser (one to many) hasMany

id, name, email .....

2 . CourseUser -- JunctionTable of course & user (many to many)

id,user_id, course_id, ....

3 . Course -- Linked to CourseUser (one to many) hasMany . Linked to video (one to many)

id,name,title.....

4 . Video -- Linked to Course

id,course_id, title, link.....

How can I access the videos as per Specific User?

Can I define hasThroughMany relation ?

Running the below in controller returns related arrays but video array shows empty items.

$courses = CourseUser::with('course.videos')->get();

dd($courses);

0 likes
3 replies
ramniksingh's avatar

No replies yet! Is there something wrong in question?

ramniksingh's avatar

User Model :

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'phone', 'address', 'is_admin'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    public function docs(){
        return $this->hasMany('App\Doc', 'id', 'doc_id');
    }

    public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'user_id');
    }

   
}

CourseUser Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class CourseUser extends Model
{
    public $timestamps = false;

    
    protected $fillable = [
        'user_id',
        'course_id',
        'start',
        'end',
        'remarks',
    ];

    public function course() {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }

    public function user() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

}

Course Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{

    public $timestamps = false;

    
    protected $fillable = [
        'title',
        'description',
        'type',
        'duration',
        'fee',
        'image',
        'remarks',
        'is_active',
    ];

    public function videos(){
        return $this->hasMany('App\Video', 'id', 'video_id');
    }

    public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'course_id');
    }

}

Video Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    protected $fillable = [
        'course_id',
        'title',
        'type',
        'description',
        'link',
        'image',
        'remarks',
        'display',
    ];

    public function course() {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }
}

Please or to participate in this conversation.