Laravel loading two models into one list

68 views Asked by At

I have 3 models: User - have many Comments Article - belongs to User Comment - belongs to User, Article In user profile I would like to show all paginated actions of given user. What is best and most effective way to do it?

2

There are 2 answers

0
AlhasanIQ On
$user= new User::find(1)
->with('comments')
->with('articles')
->get();

You might not need the get() , am not sure

0
Mehrdad Hedayati On
DB::table('users')
        ->leftJoin('comments', 'users.id', '=', 'comments.user_id')
        ->leftJoin('articles', 'users.id', '=', 'articles.user_id')
        ->paginatuse(30);