How can I append a #something to the end of the page number.
For example, this is what I want:
From:
abc.com/xxx/?page=2
To:
abc.com/xxx/?page=2#my-anchor
You can read about pagination in the docs here; Laravel Pagination.
What you are looking for is ->fragment('my-anchor').
Assuming you are using eloquent,
$links = App\YourModel::paginate(15)->fragment('my-anchor')->links();
Related
I have a Laravel query using pagination.
I want to be able to return the result based on the pagination but also get the overall total of the query and append this to the return. So for example the pagination is set to 5 but the overall total might be 20.
$query = Model::paginate(5);
$queryTotal = $query->total();
$query->append($queryTotal);
return $query;
The Laravel Paginator does this already.
You can see that when serializing results to JSON there is a total key which represents all rows matching that query.
You can also see there is a total method available from the paginator:
$results->total()
Along side other methods that can be found in the Pagination Docs
$query = Model::paginate(5);
return $query;
You can access overall total using
{{ $query->total() }}
For more Info read Paginator instance
The paginate function returns a LengthAwarePaginator object. It simply not possible to add another field to this object.
Your best option is to manually create a new collection in which you merge the LengthAwarePaginator with your newly added data.
An example would be:
$query = Model::paginate(5);
$addition = collect(['totalResult' => $query->total()]);
$queryData = $addition->merge($query);
return $queryData;
Naturally, if you just return the LengthAwarePaginator object, you can simply call the total() function, if you use it in your blade files for example.
Hope this helps!
I create compare page for products,
I need create route for compare page, How to create dynamic route in Laravel 5.3?
For example:
https://www.mywesite.com/Compare/DKP-181451/DKP-254287/DKP-254282/DKP-227429/DKP-254282
This page is 5 product for compare.
Or:
https://www.mywesite.com/Compare/DKP-181451/DKP-254287
This page is 2 product for compare.
The following will probably work:
Route::get("Compare/{any}", function ($any) {
$productsToCompare = explode("/",$any);
// Compare products.
})->where("any", ".*");
Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.
let say i have url like this
http://localhost:8080/myweb/listproduct?idcategory=3
but when i used laravel pagination link, my passing variabel by url disappear like this
http://localhost:8080/myweb/listproduct?page=2
i want laravel paginate to pass my url variable too like this
http://localhost:8080/myweb/listproduct?idcategory=3&page2
so far i already try to set baseurl like this
$listproduct = DB::table('product')->paginate(15);
$users->setBaseUrl('listproduct?idcategory=3');
but the result is like this (? not &)
http://localhost:8080/myweb/listproduct?idcategory=3?page2
i used laravel 4.2
you can use setPath()
$users->setPath('listproduct?idcategory=3');
or if you only want to add to the query string you may use
$users->appends(['idcategory' => '3'])
You can use this method to append more arguments to your pagination link:
$listproduct->appends(['idcategory' => $id])->links();
I have a search in Laravel 4. After search for sh my url is this:
http://localhost/musiter/product/search-products/24?query=sh
I also have pagination there, but the pagination for the second link for example is:
http://localhost/musiter/product/search-products/24?page=2
instead of:
http://localhost/musiter/product/search-products/24?query=sh&page=2
Any way to fix this?
The code you're looking for is:
$paginator->appends(['query' => $value])->links();
You can add to the query string of pagination links using the appends method on the Paginator:
<?php echo $users->appends(array('sort' => 'votes'))->links(); ?>
This will generate URLs that look something like this:
http://example.com/something?page=2&sort=votes
If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method:
<?php echo $users->fragment('foo')->links(); ?>
This method call will generate URLs that look something like this:
http://example.com/something?page=2#foo
You can also check [Laravel documentation].1