How do i join two queries and then paginate

52 views Asked by At
    $data = Category::join('articles', function ($join) {
       $join->on('categories.id', 'articles.category_id')
        ->where('categories.type', 'news')
        ->where('articles.status', '1');
    })->get()->sortByDesc('updated_at')->paginate(5);

I have 2 variables both querying the same model. I want to now merge these 2 separate datasets into a variable called $articles and then paginate on it.

2

There are 2 answers

0
engrhussainahmad On BEST ANSWER

Remove the get() method from the code like:

$data = Category::join('articles', function ($join) {
    $join->on('categories.id', 'articles.category_id')
    ->where('categories.type', 'news')
    ->where('articles.status', '1');
})->sortByDesc('updated_at')->paginate(5);

It should work.

0
joun On

When joining queries and paginate in controller, to view the output in paginate you should include this under you for loop output:

{{ $data->links() }}