I have a Laravel aplication, with standard Authentication implemented.
I would like to get the paginated list of the users in a table, but I get this error:
BadMethodCallException in Macroable.php line 81: Method paginate does not exist.
Here is the code:
$users = User::all()->paginate(25);
If I use this without calling the paginate() method, everything works well.
Am I doing something wrong?
I searched a lot, but I cannot find the answer.
Try this code:
$users = User::paginate(25);
The thing is paginate() is kind of doing get() for you. And all() does it too.
Related
I am using laravel 7 for my project, and I am trying to display unique items from the database with pagination, but I noticed the pagination do not work with the unque method. Please how can I go around this? Below is my code:
$bid = Bid::latestFirst()->get();
$bids = $bid->unique('item_id');
$bids->values()->paginate(10); //This is where I get the error: BadMethodCallException
//Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
You can use distinct() like so
Bid::distinct('item_id')->latestFirst()->paginate(10);
I am trying to make a filter in laravel. This following filter works
$posts= Post::where('category',$request->category)->orderBy('id','desc')->paginate(10);
But when I try to do something like this
public function index(Request $request)
{
$posts= Post::where('category',$request->category)->get();
$posts->latest()->paginate(10);
dd($posts);
It doesn't work. Can someone explain why is this and provide me the code that works. My project have multiple filter.
Error
Because $posts = Post::all(); already execute a query.
Post::where('category',$request->category)->latest()->paginate(10)->get();
would be what you want.
A note:latest requires the created_at column
You should go
$posts = Post::where('category',$request->category)->latest()->paginate(10);
the get request is unnecessary as the paginate will execute the query.
The first one makes the query by pagination i.e fetch 10 records per constructed page
For the second one, based on observation, you most likely have encountered at least 2 errors:
The first, on the line that used the get method because that method requires at least one parameter.
Type error: Too few arguments to function Illuminate\Support\Collection::get()
The other since its a collection, and since there is nothing like paginate or latest method on collection therefore throws other errors. You should check Collection's Available methods to have a glimpse of the methods allowed on collection.
One of the best solutions is to simply order the result when making the query:
Blog::where('category',$request->category)
->orderBy('created_at', 'desc') //you may use also 'updated_at' also depends on your need
->paginate(10);
This way you have the latest coming first int the pagination and also having not worrying about paginating a collection
BadMethodCallException in Macroable.php line 74:
Method orWhere does not exist.
$category = $categories->where('Node_ID', (explode('.', $cat{$title_id})[0]))
->orWhere('Node_Path', $cat->{$category_name})
->first();
If I try without "orWhere" works, if I use it, throws an Error. Someone knows where is the mistake?
You are trying to use orWhere on collections, thats why its showing you the error. You should use this on model like this (taking Category as a Model):
$category = Category::where('Node_ID', (explode('.', $cat{$title_id})[0]))
->orWhere('Node_Path', $cat->{$category_name})
->first();
See Laravel Docs for orWhere()
Hope this helps!
I am writing the below code to paginate record...
$Categories = \App\Models\Skill\Category_Model::all()->paginate(15);
Then I got a runtime error says:
Method paginate does not exist.
Am I missing something ?
You cannot paginate that way, all() function gives you a Collection already.
You need to use \App\Models\Skill\Category_Model::paginate(15) to do that.
So I can't for the life of me figure out why I can't use orderBy. I'm getting the error:
Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy()
Here is my code:
Route::get('/teams', function(){
$page = 'Teams';
$teams = App\Team::all()->orderBy('teamFirstName')->get();
return view('teams')->with('page', $page)->with('allTeams', $teams);
});
I have tried removing the ->get(); I have tried using just Team::all. I'm using a Laravel cheat sheet and I seem to be following the Model syntax. I have also double checked that it is the right column name in my DB and have even tried using just id.
If I remove the ->orderBy() the query works fine and I can get all of the teams. So what am I doing wrong?
It's because all() returns a Collection which does not have an orderBy method (though it does have a sortBy method which you could use). The following is a bit simpler though and should perform better.
$teams = App\Team::orderBy('teamFirstName')->get();
Once you call all(), the query is ran and results are fetched and any ordering done after this would be done by PHP. It's usually best to let the database handle the ordering though so use orderBy() first before fetching the results with get().
This line:
$teams = App\Team::all()->orderBy('teamFirstName')->get();
Need to be:
$teams = App\Team::orderBy('teamFirstName')->get();
Note:
user3158900 has a good explanation.