user channel video laravel eloquent relationship

Published 6 days ago by pedropankaj

I am struk at one point. i have a three table User, channel, video

tables user ---> channel user ---> videos want to access channel ---> videos with User details

the condition is

  1. user has one channel
  2. user has many videos
  3. channel has many videos

i want to get all the videos of channel has and with user details

Snapey
Snapey
6 days ago (678,595 XP)

So if you create relationships

User.php

public function channel()
{
    return $this->belongsTo(Channel::class);    // depends on channel_id column in users
} 

public function videos()
{
    return $this->hasMany(Video::class);   //depends on user_id column on videos table

Channel.php

public function user()
{
    return $this->hasOne(User::class);      // depends on channel_id column in users
}

public function videos()
{
    return $this->hasManyThrough(Video::class, User::class);
}

Video.php

public function users()
{
    return $this->belongsTo(User::class);        // depends on user_id column on videos table
}

https://laravel.com/docs/5.5/eloquent-relationships#has-many-through

Please sign in or create an account to participate in this conversation.