I have a fairly simple query which I load and add an element to the object and then sort it based on that custom element. I'd like to take 20 records or paginate it but when I do so, alot of data vanishes.
This is my code. The piece below gets all the records.
$fighters = Fighter::all();
The below code gets the points which is in a function and adds it to the fighter, fighterPoints does not initially exist in the collection, it is created and populated below.
foreach ($fighters as $fighter) {
$fighter->fighterPoints = $fighter->getFighterPoints($fighter->id);
}
Then i'd like to sort everything by those fighterPoints with the below function.
$sorted = $fighters ->sortByDesc(function ($item, $key) {
return $item->fighterPoints ;
});
When i do this i get all the records which are around 9000, it then sorts on the fighterPoints correctly:
The first record being something like [FighterName, 21309]
When i do $fighters = Fighter::paginate(20); it simply starts with [FighterName384, 200] which should be [FighterName, 21309] and just 20 results. The same thing happens with ::take(20) method.
What am I doing wrong here? I am laravel 8.
You want to paginate your $sorted variable right?!
To do that you have to
use Illuminate\Pagination\Paginator; // add
use Illuminate\Support\Collection; // add
use Illuminate\Pagination\LengthAwarePaginator; //add
...(your code)
// add
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
then, use that $sorted this way:
$fighters = $this->paginate($sorted);
reference:https://www.itsolutionstuff.com/post/laravel-6-paginate-with-collection-or-arrayexample.html
—————- EDIT ————
Sorry I misunderstood your question!
If you want to order eloquent, here is how you do it
$fighters = Fighter::orderBy('fighterPoints', 'desc')->paginate(20);
I hope that’s what you are looking for!
Related
I have a function that looks for possible boxes that can carry the article.
public static function get_possible_boxes($article,$quantity)
{
$i = 0;
$possible_boxes = array();
$total_weight = $articles->grams * $quantity;
$boxes = Boxes::all();
foreach($boxes as $box)
{
if($total_weight+ $box->grams < $box->max_weight)
{
$possible_boxes[$i] = $box;
$i++;
}
}
return collect($possible_boxes);
}
This gives me a collection with boxes that can carry my items.
Now I should check if the ID of the box selected by the customer exists. If it does not exist, it will pick the first valid one.
This is where I am stuck. I have tried to use puck:
public function someotherfunction(){
...
$boxes = get_possible_boxes($something,$number);
$valid_box = $boxes->where("id", $request->selected_box)->pluck("id");
if(!$valid_box){
$valid_box = $boxes[0]
}
...
This works if the selected box cannot be used. The function pluck only gives me the id, clearly it is not the function I am looking for and I have already read the Laravel documentation.
So the question is, how do I get the correct eloquent model ?
You're looking for the first() method.
$valid_box = $boxes->where("id", $request->selected_box)->first();
Alternatively, if you rework your get_possible_boxes() method to return a Illuminate\Database\Eloquent\Collection instead of a plain Illuminate\Support\Collection, you could use the find() method, like so:
Function:
public static function get_possible_boxes($article,$quantity)
{
$total_weight = $article->grams * $quantity;
$boxes = Boxes::all()->filter(function ($box) use ($total_weight) {
return $total_weight + $box->grams < $box->max_weight;
});
return $boxes;
}
Find:
$boxes = get_possible_boxes($something, $number);
$valid_box = $boxes->find($request->selected_box) ?? $boxes->first();
And you could probably squeeze out a little more performance by adding the weight condition as part of the SQL query instead of filtering the collection after you've returned all the boxes, but I left that up to you.
What you want is probably filter.
$valid_box = $boxes->filter(function($box) use ($request){
return $box->id == $request->selected_box;
});
if($valid_box)...
I should note that if you don't want $valid_box to be a collection, you can use first instead of filter in the exact same way to only get the object back.
It could be done in many ways but I would rather use the following approach:
$boxes = get_possible_boxes($something,$number)->keyBy('id');
$valid_box = $boxes->get($request->selected_box) ?: $boxes->first();
I have set of Ids as an array. It is out put of a complex search logic. For example my array is :
array(2,5,1,8,9,12,83,32);
Each of these IDs are valid and represents posts. So what I do is I just loop them in a for each loop and collect all data to a final array. Example :
foreach($ArrayOfIds as $id)
{
$posts[] = Post::findOrFail($id);
}
then I send this $posts to my view. Is it possible to implement laravel pagination in this scenario.? Keep in mind that I can't play any more with $ArrayOfIds.
Yes it's possible, example using Illuminate\Pagination\LengthAwarePaginator
public function showPost()
{
$posts = [];
foreach($ArrayOfIds as $id) {
$posts[] = Post::findOrFail($id);
}
$perPage = 6;
$paginator = new LengthAwarePaginator($posts, count($posts), $perPage);
$items = $paginator->getCollection();
return $paginator->setCollection(
$items->forPage($paginator->currentPage(), $perPage)
);
}
At first, I instantiate LengthAwarePaginator class and feed it with the arguments needed, see here. And then, as I need to set which posts belongs to which page, I then use forPage method from Illuminate/Support/Collection.
Something like that.
Why the paginate method doesn't work on this example ?
$shopIds = Follower::whereUserId($user->id)->orderBy('created_at','desc')->get()->pluck('shop_id')->toArray();
$shops = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->paginate($this->perPage)->sortBy(function($likes) {
return $likes->count();
});
dd($shops);
Thank's for help ;)
The paginate is just working fine but the sortBy method is creating the problem for you because when you use sortBy it returns a new collection.
So finally your $shops is an instance of Illuminate\Support\Collection not of Illuminate\Pagination\LengthAwarePaginator.
You can try it as:
$paginated_shops = Shop::whereIn('id',$shopIds)
->with('deals')
->with('deals.likes')
->paginate($this->perPage);
$shops = $paginated_shops->sortBy(function($likes) {
return $likes->count();
});
$shops = new LengthAwarePaginator($shops, $paginated_shops->total(), $paginated_shops->perPage());
Remember to add use statement at the top of the class as:
use Illuminate\Pagination\LengthAwarePaginator;
Thank you guys!
I fix it like that
$shopIds = Follower::whereUserId($user->id)->orderBy('created_at','desc')->get()->pluck('shop_id')->toArray();
$shopIds = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->get()->sortBy(function($likes) {
return $likes->count();
})->pluck('id')->toArray();
$orderedIds = implode(',',$shopIds);
$shops = Shop::whereIn('id', $shopIds)->whereNotIn('user_id', [$user->id])->orderByRaw(\DB::raw("FIELD(id, ".$orderedIds." )"))->paginate($this->perPage);
dd($shops);
and now I have an instance of Illuminate\Pagination\LengthAwarePaginator
Thank's
Like #Amit mentionned, $shops is a collection, thus not a LengthAwarePaginator. You are going to have to build the paginator and slice it yourself... not too complicated...
For example, let's assume you are hitting your route, calling your controller... then returning results to a view... Now that route is going to get requested with the pagination info in the request, you job is to get the result $shops into an array, slice the array into the right result for the page and return that to your view... I have not run this code... so take this as an example... but it will look something like:
public function yourController{
// This will return a collection... we will convert it to array later
$shops = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->paginate($this->perPage)->sortBy(function($likes) {
return $likes->count();
});
$shopsArray = $shops->toArray();
// Here we deal with the slicing... you need to slice the page you are
// at the paging info is in the request...
// this basically gets the request's page variable... or defaults to 1
$page = Paginator::resolveCurrentPage('page') ?: 1;
// Assume 15 items per page... so start index to slice our array
$startIndex = ($page - 1) * 15;
// Length aware paginator needs a total count of items... to paginate properly
$total = count($shopsArray);
// Eliminate the non relevant items by slicing the array to page content...
$results = array_slice($shopsArray, $startIndex, 15);
$result = new LengthAwarePaginator($results, $total, 15, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
return view('yourView', compact('result'));
}
So I know how to paginate using paginate() and I know how to filter based on an Accessor (a where() on the collection). However, paginate takes in a query builder and where() on a collection returns a collection.
So if I want to get a bunch of items / filter by a custom attribute and then paginate the result set....how do i do that??
Accessor:
public function getRequiredToReportAttribute()
{
// return boolean based off of complicated business logic
}
index method:
public function index()
{
//what im doing (redacted)
$employers = (new App\Employers')->paginate($this->perPage);
// what I would like to be doing
$employers = (new App\Employers)->where('required_to_report', '=', true)->paginate($this->perPage);
return $this->sendResponse($employers);
}
In the case that you want to work with accesors, you could by iterating the collection after you get your query, something like this:
$result = Model::get()->filter(function($item) {
return $item->require_to_report === true;
});
Here you have all records of your model and then you could create a manual paginator:
$paginator = new Illuminate\Pagination\Paginator($result, 10);
you have with this approach a weakness when you have too many records, the performance could be affected.
Based off of Jose Rojas answer and this post I built a LengthAwarePaginator for a collection filtering on an attribute accessor. Here's an example of how to do it:
$collection = Model::all();
//Filter your collection
$filteredItems = $collection->filter(function($col) {
return $col->require_to_report === true;
});
// Setup necessary information for LengthAwarePaginator
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$pageLimit = 20;
// slice the current page items
$currentItems = $filteredItems->slice(pageLimit * ($currentPage - 1), pageLimit)->values();
// you may not need the $path here but might be helpful..
$path = "/api/v1/employers";
// Build the new paginator
$paginator = new LengthAwarePaginator($currentItems, count($filteredItems), $pageLimit, $currentPage, ['path' => $path]);
return $paginator;
Having problems getting my pagination to work in Laravel 5.2 I use a foreach to generate a list of objects where each object has a certain ranking. (competition)
The first query I used was this one:
$goedeDoelen = GoedDoel::orderBy('punten', 'desc')->simplePaginate(5);
This worked pretty ok, only problem was that my ranking would reset everything I would go to a different page.
Example: Page 1 has objects from rank 1 - 5, page 2 should have ranks 6-10. By using the first Paginate method, the second page would have objects starting from 1 again.
I have tried to work around this by adding the ranking as an extra attribute to my Eloquent collections.
$ranking = GoedDoel::orderBy('punten', 'desc')->get();
foreach($ranking as $key => $item) {
$item->ranking = $key+1;
}
After that I tried to use ->simplePaginate() on my updated collection. This gave an error.
I have created a custom Paginator.
$goedeDoelen = new Paginator($ranking, 5);
This isn't working as intended. When I go to my second page, the URL messes up and goes to another view.
How can I make sure the Paginator knows what my current URL is to which it has to apply the ?page=2
You need to use the paginate() method.
$goedeDoelen = GoedDoel::orderBy('punten', 'desc')->paginate(5);
{!! $goedeDoelen->links() !!}
The following Code illustrates manual pagination in Laravel
Sample Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use App\Models\UserRechargeDetails;
class PaginateController extends Controller
{
//
public function index(Request $request)
{
$user_1 = new UserRechargeDetails;
// Get records from Database
$items = $user_1->all();
// Store records in an array
$records = [];
$i = 0;
foreach($items as $item)
{
$records[$i][0] = $item->user_name;
$records[$i][1] = $item->rech_mobile;
$i++;
}
// Current page for pagination
$page = $request->page;
// Manually slice array of product to display on page
$perPage = 2;
$offset = ($page-1) * $perPage;
$data = array_slice($records, $offset, $perPage);
// Your pagination
$final_data = new Paginator($data, count($records), $perPage, $page, ['path' => $request->url(),'query' => $request->query(),]);
/*
For Display links, you may add it in view page
{{ $data->links('pagination::bootstrap-4') }}
*/
return view('admin.pagination_new', ['data' => $final_data, 'j' => 1]);
}
}