Laravel 5 Merge LengthAwarePaginator - laravel-5

I have 2 objects LengthAwarePaginator from 2 different queries that I'd like to merge to display on the same page/table.
As soon as I merge the 2 objects, I'm losing most of the properties such as the total of elements and can't use the Laravel 5 render() function:
$final = $res_query_1->merge($res_query_2)
Any thoughts?

public function merge(LengthAwarePaginator $collection1, LengthAwarePaginator $collection2)
{
$total = $collection1->total() + $collection2->total();
$perPage = $collection1->perPage() + $collection2->perPage();
$items = array_merge($collection1->items(), $collection2->items());
usort($items, array($this, 'cmp'));
$paginator = new LengthAwarePaginator($items, $total, $perPage);
return $paginator;
}

Related

Laravel 5.4 LengthAwarePaginator

My brain suddenly crashed on this one. Anyone care to help me is highly appreciated.
This is LengthAwarepaginator in laravel 5.4
Here is the code.
$collection = [];
foreach ($maincategories->merchantCategory as $merchantCat) {
foreach ($merchantCat->merchantSubcategory as $merchantSub) {
foreach($merchantSub->products as $products){
$collection[] = $products;
}
}
}
$paginate = new LengthAwarePaginator($collection, count($collection), 10, 1, ['path'=>url('api/products')]);
dd($paginate);
It displays perfectly but the problem is the items is 100. That's all my items and I specify it correctly. I need to display only 10.
Base on LengthAwarePaginator constructor. Here is the reference.
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
Here is the screen shot.
Where did I go wrong? TY
When manually creating a paginator, you have to slice the result set yourself. The first parameter to the paginator should be the desired page of results, not the entire result set.
From the pagination documentation:
When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the array_slice PHP function.
I would suggest using a Collection to help out with this a little:
// ...
$collection = collect($collection);
$page = 1;
$perPage = 10;
$paginate = new LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page,
['path' => url('api/products')]
);
For the heavy select and to avoid any multiple select to calculate the total from the table we unable to use model paginate
use Illuminate\Pagination\LengthAwarePaginator;
in your controller function
if(!isset($input["total"])){ //initial request
$total = //Find the total only one time.
$request->request->add(['total' => $total]); //Add it in the request so that we can print it in blade
}else
$total = $input["total"]; //After initial request
$currentPage = LengthAwarePaginator::resolveCurrentPage(); //page variable from GET or POST
$perPage = 30; //Page Length
$offset = ($currentPage - 1) * $perPage; //find the offset to pass in query
$specificRecords = /*Collect specific page records in array
if mysql then Select * from table limit $perPage offset $offset
if ms sql then OFFSET {$offset} ROWS FETCH NEXT {$perPage} ROWS ONLY */
$records = new LengthAwarePaginator($specificRecords,$total,$perPage,Null,[ "path" => "/pagepath" ]);
in blade:
<center>{{$records->appends(Request::except([ 'page','_token' ]))->links()}}</center>
Check Page and total variable in page tags ensure you added page in except list :)

How to create a paginator?

I've checked out the rather thin docs, but still unsure how to do this.
I have a collection. I wish to manually create a paginator.
I think I have to do something like, in my controller:
new \Illuminate\Pagination\LengthAwarePaginator()
But, what params do I need and do I need to slice the collection? Also how do I then display the 'links' in my view?
Could someone post a simple example how to create a paginator?
Please note, I don't want to paginate eloquent, eg. User::paginate(10);
Take a look at the Illuminate\Eloquent\Builder::paginate method for an example on how to create one.
A simple example of doing one using an eloquent model to pull out the results etc:
$page = 1; // You could get this from the request using request()->page
$perPage = 15;
$total = Product::count();
$items = Product::take($perPage)->offset(($page - 1) * $perPage)->get();
$paginator = new LengthAwarePaginator(
$items, $total, $perPage, $page
);
The first parameter accepts the results to display on the page that you're on
the second is the total number of results (The total number of items you're paginating, not the total number of items you're displaying on that page)
the third is the number per page you want to display
the fourth is the page that you're on.
You can pass in extra options as a fifth parameter if you want to customise things as well.
The links you should just be able to generate using the ->render() or ->links() method on the paginator as you would if you used Model::paginate()
With an existing collection of items you could do this:
$page = 1;
$perPage = 15;
$total = $collection->count();
$items = $collection->slice(($page - 1) * $perPage, $perPage);
$paginator = new LengthAwarePaginator(
$items, $total, $perPage, $page
);
You can create a Paginator like this:
$page = request()->get('page'); // By default LengthAwarePaginator does this automatically.
$collection = collect(...array...);
$total = $collection->count();
$perPage = 10;
$paginatedCollection = new \Illuminate\Pagination\LengthAwarePaginator(
$collection,
$total,
$perPage,
$page
);
According to the source code for LengthAwarePaginator (constructor)
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
$this->total = $total;
$this->perPage = $perPage;
$this->lastPage = (int) ceil($total / $perPage);
$this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}
See more about LengthAwarePaginator
To display links in the view:
$paginatedCollection->links();
Hope this helps!

Pagination with array not working in laravel 5.1

I have to set pagination in array result.
Here is my code.
My Controller code
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
public function getCVList(){
.
.
.
$jobseeker1 = array_merge($jobseekers, $apps_array);
// in $jobseeker1 there are 6 result.
$jobseeker = $this->paginate($jobseeker1, 3);
return view('frontend.CVList', compact('jobseeker'));
}
public function paginate($items, $perPage, $pageStart = 1) {
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
In blade temoplate i used rander() method and thare are also display pagination. But in all page display same record.
Thanks....
This is because you are not reading the page number clicked in the paginator, you are setting "3" always as the page to display. Try this:
//include the request
use Illuminate\Http\Request;
Now, read the current page:
public function getCVList(Request $request){
$perPage = 3;
// read the page number. When page number is not presented, then you
// set it as 0
$page = $request->get('page', 0);
$page = ($page == 0)? ($page * $perPage) : ($page * $perPage) - $perPage;
// now, calling the paginator do magic dynamically
$jobseeker = $this->paginate($jobseeker1, $perPage, $page);

Paginate merged queries

I have two queries:
$posts = Post::all();
$comments = Comment:all();
Which I later merge:
$merge = $posts->merge($comments);
$all = $merge->sortByDesc(function($result) {
return $result->created_at;
});
How can I paginate the result of the merge? Doing $all->paginate(25) does not work.
Laravel 5:
In Laravel 5 it can be easily done by instantiating paginator:
$paginator = new Illuminate\Pagination\Paginator($all, $perPage, $currentPage);
You may try this (Version-5x):
// Add this at the top of your class
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
// Then use following code:
$perPage = 10;
$currentPage = app('request')->get('page') ?: 1; // or $request->get('page') if available
$paginator = new Paginator($all, $all->count(), $perPage, $currentPage);
For 4.2 try this syntax:
$items = $all->toArray();
$total = count($items);
$perPage = 10;
$paginator = Paginator::make($items, $total, $perPage);
Laravel 5:
You can use spatie/laravel-collection-macros.
$posts = Post::all();
$comments = Comment:all();
return $comments->merge($posts)->paginate();

How can I paginate an array of objects in Laravel?

I'm building an application using Laravel 4.2. I have a model for units and another for users and pivot table user_units. Every user in this application can select a unit and add it to his favorite list then he can publish this unit with his information as an ad.
I want to select all units published by all users
The user_units (pivot) table has the following columns:
id
user_id
unit_id
publish
adtype
addinfo
created_at
updated_at
With relations methods on models
public function users() {
return $this->belongsToMany('User', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
public function units() {
return $this->belongsToMany('Unit', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
My query to select all published units by all users
// Get all published units by users for sale.
$users = User::all();
$publishedSaleUnits = [];
foreach($users as $user){
$userUnits = $user->units()->orderBy('adtype', 'desc')->get();
if(count($userUnits)){
foreach($userUnits as $unit){
if($unit->pivot->publish == 1 && $unit->unit_purpose_id == 1){
if( $unit->pivot->adtype ){
//push all featured ads onto the beginning of array
array_unshift($publishedSaleUnits, $unit);
}else{
//push all normal ads onto the end of array
array_push($publishedSaleUnits, $unit);
}
}
}
}
}
Now I got the result but I can't use pagination with results because it's an array of objects.
So is there any better solution to get all published units by user with pagination?
according to this article
https://www.itsolutionstuff.com/post/how-to-create-pagination-from-array-in-laravelexample.html
you can paginate your array by creating a custom method and using LengthAwarePaginator
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class PaginationController extends Controller
{
public function index()
{
$myArray = [
['id'=>1, 'title'=>'Laravel CRUD'],
['id'=>2, 'title'=>'Laravel Ajax CRUD'],
['id'=>3, 'title'=>'Laravel CORS Middleware'],
];
$data = $this->paginate($myArray);
return view('paginate', compact('data'));
}
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);
}
}
Your approach to query the data is extremely inefficient. Fetch your data in one query. Nested traversing is not only hard to read but also a performance killer.
To the pagination problem:
Laravel provides a Pagignator Factory. With it you will be able to build your own Paginator with your own data.
It's as easy as
$units = Paginator::make($unit, count($unit), 10);
if you're using the Facade. Otherwise Illuminate\Pagination\Factory is the class you are looking for.
You can try my code with your own array,
$page = isset($request->page) ? $request->page : 1; // Get the page=1 from the url
$perPage = $pagination_num; // Number of items per page
$offset = ($page * $perPage) - $perPage;
$entries = new LengthAwarePaginator(
array_slice($contact_list, $offset, $perPage, true),
count($contact_list), // Total items
$perPage, // Items per page
$page, // Current page
['path' => $request->url(), 'query' => $request->query()] // We
need this so we can keep all old query parameters from the url
);
I got a better solution to paginate array result and I found the answer here
Paginator::make function we need to pass only the required values instead of all values. Because paginator::make function simply displays the data send to it. To send the correct offset paginated data to the paginator::make, the following method should be followed
$perPage = 5;
$page = Input::get('page', 1);
if ($page > count($publishedSaleUnits) or $page < 1) { $page = 1; }
$offset = ($page * $perPage) - $perPage;
$perPageUnits = array_slice($publishedSaleUnits,$offset,$perPage);
$pagination = Paginator::make($perPageUnits, count($publishedSaleUnits), $perPage);
this code work for me on laravel 8
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
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);
}
according to this refrence https://www.itsolutionstuff.com/post/how-to-create-pagination-from-array-in-laravelexample.html

Resources