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);
Related
I am working a project and I would want to use a where clause, paginate and then sort in the collection in specific order. I have tried the result below but keeps throwing the errors below Method:
Illuminate\Database\Eloquent\Collection::links does not exist. (View:
/Applications/XAMPP/xamppfiles/htdocs/vermex/resources/views/equipments.blade.php)
The Product model is where I am getting the data and store in a variable called $equipment. If there is a better way of doing this, please help.
public function equipments()
{
$equipments = Product::where('product_category_id', 3)->paginate(2)-
>sortByDesc('id');
return view('equipments', compact('equipments'));
}
Try putting the orderBy before the paginate
$equipments = Product::where('product_category_id', 3)->orderBy('id', 'desc')->paginate(2);
sortByDesc is a collection method.
paginate will need to be last for links to be available in the blade view.
I'm fetching record for multiple section in a view. To show complete data I am simply doing
$data = Data::all();
Then simply getting counts for each section as required.
For example:
{{count($data->where('status', 'HOLD'))}}
For another section on the same view I want to show result with Groupy
For Example:
#foreach($data->paginate()->groupBy('user_id') as $byUser)
#endforeach
Then show the pagination
{{$data->links()}}
I have already tried by separating the result in the controller and Calling the data using different methods.
$data = Data::all();
$userData = Data:paginate(5)->groupBy('user_id');
return view('data.all', compact('data', 'userData'));
This is working well without pagination. After adding pagination I am getting below error.
Method Illuminate\Database\Eloquent\Collection::paginate does not exist. (View: C:\xampp\htdocs\project\resources\views\data\all.blade.php)
Is there any way to work it out? Thanks.
Check this line:
$data = Data::all();
Here you are already executing the query, this means, a Collection instance is returned. That's why you can't use the paginate() method, because that is a QueryBuilder method.
Try this:
$userData = Data::groupBy('user_id')->paginate(5);
return view('data.all', compact('data', 'userData'));
To do pagination in laravel, the easiest way is by:
$items = ModelOfItem::orderBy('my_order_field', 'asc')->paginate(10);
However, in this pagination, I need to relate data from another table to this page. Currently, I do it by:
$item_ids = ModelOfItem::orderBy('my_order_field', 'asc')->take(10)->list('id');
$related_items = ModelOfRelatedItem::whereIn($item_ids)->get();
However, it needs to query database twice on data that I already have in hand, and it is painful to handle page after first page. Is there a way I can get a list of id from the pagination result so I can use to directly query on the second table?
P.S. It is an old project so it is still using Laravel 4.2.
Why don't you use joins? You can join the tables & call ->paginate(10) as the end method. if you still want this approach for whatsoever reason.. You can call ->lists('id') on paginator object as well.. like this:
$items = ModelOfItem::orderBy('my_order_field', 'asc')->paginate(10);
$item_ids = $items->lists('id');
$related_items = ModelOfRelatedItem::whereIn($item_ids)->get();
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.
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.