3
votes

I have this code

$post = DB::table('posts')
->join('post_images','post_images.post_id','=','posts.id')
->join('categories','categories.id','=','posts.category_id')
->orderBy('posts.created_at','desc')->paginate(15)
->select('posts.*','categories.title as category', 'post_images.image')
->get();

But I am getting Method Illuminate\Support\Collection::select does not exist..

I need to paginate all the posts

1

1 Answers

3
votes

What you get back from paginate() is already a collection and no longer the query builder, so you can't call select() on it. reorder your method calls and you should be fine.

$post = DB::table('posts')
->join('post_images','post_images.post_id','=','posts.id')
->join('categories','categories.id','=','posts.category_id')
->orderBy('posts.created_at','desc')
->select('posts.*','categories.title as category', 'post_images.image')
->paginate(15);

Also, I would suggest, that you create a model and relationships if you are using Laravel/Eloquent - makes the whole thing easier. If you then need some extra magic that you want to have on multiple places, you should check out model scopes 😉