I followed the Laravel 5.4 from Scratch series and am currently trying to implement pagination into the blog example.
It's easy to get pagination for the homepage:
Post::latest()->filter(request(['month', 'year']))->simplePaginate(5);
Problem: paginate() or simplePaginate() cannot be called like this:
public function index(Tag $tag){
$posts = $tag->posts->paginate(5);
return view('posts.index', compact('posts'));
}
So when I visit /posts/tags/{tag}, it tells me method paginate() doesn't exist. How am I supposed to paginate the results in that case, so that I can still hand over the Eloquent model of Post (which I need to display all the details)?
Thank you very much!
Related
Im using Laravel v7 and i have a question about pagination.
So far im using 2 routes, 1rst to return a view with all rows from database, and 2nd receives an input and returns that view with the rows filtered by that input value.
But im using pagination, and on the 2nd route, when i try to go to 2nd page it gives me an error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
I've tried to change my form method to GET but i need that the token doesn't appear on the page URL and beside that, when i go to 2nd page, it returns all the rows again.
Thats my code so far:
Routes:
Route::get('concessions', 'ConcessionController#index')->name('concessions.index');
Route::post('concessions/search', 'ConcessionController#search')->name('concessions.search');
Controller
class ConcessionController extends Controller
{
public function index()
{
$concessions = DB::table('concessions')->paginate(12);
return view('admin.concessions.index', compact('concessions'));
}
public function search(Request $request)
{
$name = $request->name;
$concessions = Concession::where('name', 'like', '%' . $name . '%')->paginate(12);
return view('admin.concessions.index', compact('concessions', 'name'));
}
}
Any way to do that?
Laravel pagination only works with get parameters.
You should use GET method for your search page. POST requests aren't meant for the purpose of displaying data. Why? There are many reasons, but to be short I will give you three examples :
1. When you access the first page, you get the data by GET request,
not POST request. So if you want to use POST request, you
need to access the page as POST request by sending data with
POST method.
2. With GET parameters, let's say you are on 5th page - you can
copy the link and paste it to friend and he will be able to view
the same content as you. With POST this is impossible.
3. You can not use back button with POST requests, if you manage to
get pagination to work.
POST requests are useful when you need to submit data to the server, in order to create new record, for example.
So I suggest you to change your route type to GET.
From my perspective, if you change your route code similar to below code, it will work properly with both methods of GET and POST.
Route::any('concessions/search', 'ConcessionController#search')->name('concessions.search');
normally i use this:
$users = \App\Users::where('age', '>=', '20')->paginate(20);
But if I use raw querys or use the get(); method from querybuilder, I got an array not a collection or a paginator instance as result.
How can I get my ->links(); from that array result?
Hope all is explained well. :-)
KR,
Marcel
Write{{ $users->links() }} in your view page. According to your query, pagination will show while your data return more than 20 rows.
For better understand Read this doc
I'm trying to add index to Algolia using Laravel Scout based on a condition. For example I have a Article model and I only want to add this article to Algolia if the article is active. My first approach was this:
public function toSearchableArray()
{
if($this->active) return $record;
return [];
}
this only adds the active records but still attempts to add empty arrays which is considered as Operation in algolia ( I will be charged for it). The second approach was to use shouldBesearchable() function from scout:
public function shouldBeSearchable()
{
if($this->active) return true;
return false;
}
This doesn't work with php artisan scout:import "App\Article". Has anyone faced a similar problem?
It was a bug in Laravel Scout, shouldBeSearchable is not release yet (on master branch) so you may experience some issue like this one.
Although, good news: it was just fixed by this PR.
https://github.com/laravel/scout/pull/250
How to use custom pagination in laravel 5.2 using Illuminate\Pagination\LengthAwarePaginator , i think this gives more flexibility than other class according to thier doc. But couldn't figure it out how to do that . So i need help on this . Kindly guide me .
Code in controller :
public function index(Request $request )
{
$data=array();
//**** fetch data starts
$countries_db = DB::table('location_country as lc');
$countries_db=$countries_db->select(DB::raw('lc.id,lc.country_name,lc.country_3_code,lc.country_2_code,lc.lat,lc.lng,lc.published'));
$pagi_country=$countries_db;
//**** fetch data ends
//***** pagination code starts
$pagi_country = $pagi_country->paginate(3);
$pagi_country->setPath('country/');
$data['pagi_country']=$pagi_country;
$data['useinPagiAr']=$useinPagiAr;
//***** pagination code ends
return view('admin.country.countrylist', $data);
}
Code in View
we will get data from this variable $pagi_country passing it to foreach loop , and printing data. To print the pagination link following code is needed
echo $pagi_country->appends($useinPagiAr)->render();
I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!