Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Illuminate\Support\Collection::select does not exist

Tags:

php

laravel

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

like image 952
Alezco05 Avatar asked Oct 22 '25 15:10

Alezco05


1 Answers

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 😉

like image 55
Thomas Venturini Avatar answered Oct 25 '25 04:10

Thomas Venturini