I actually have the same error as of now.
Laravel pagination links not working
I have this code on the view that is displaying for pagination correctly
{{$products->links()}} The link created are correct:
http://demo.com/default?page=2 However, the items that is listed remains to be on the first page only. Could there be anything wrong?
FYI, in the controller I have this to generate the data
$products = \App\Product::paginate(1);
Yep I have same error, Did you find an answer to it ? not sure if we need to use that query string variable 'page' in query somewhere. But not able to find it in laravel documentation. Using laravel 5.6
it should work as described. You dont need to do anything special with the querystring
@hadesunseenn show your controller code
@jlrdw this is the code in controller
$tests = LabTest::orderby('id', 'desc')->paginate(10); return view('labTest.index', compact('tests'));
and this is the code in template {!! $tests->links() !!}
It does create pagination links in view but when I click on any link it does not change any records in view. But just changes the query string variable 'page' to that paginate number.
In controller do
dd($tests);
and show results
and try
{{ $tests->links() }}
when I print_r($tests);
looks like this variable has some issue
[currentPage:protected] => 1
it is always '1'. When I click on any pagination link this variable has always the same value.
You need to fix this:
LabTest::orderby('id', 'desc')->paginate(10);
seems you aren't getting results. How many results are there in that.
Show model
I can see results in view and pagination links are there. But the results does not change when I click on any pagination link and also the active pagination link is always first one. So only thing which is being updated is the query string variable 'page'.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LabTest extends Model
{
protected $table = 'lab_test';
protected $fillable = [
'user_id', 'test_name'
];
}
?>
Did you take out the exclamation marks like I showed? And try
{{ $tests->links() }}
And neat, how did you do the blue background?
yep I tried this too. There is not much diff between {{ and {!!
I did not know to format code in comments so I used 'pre' tag haha. But now I know.
{{ $tests->links() }}
I tried but seriously dont know, what I am doing wrong. Checked the documentation too
so far this is model
namespace App;
use Illuminate\Database\Eloquent\Model;
class LabTest extends Model
{
protected $table = 'lab_test';
protected $fillable = [
'user_id', 'test_name'
];
}
This is controller
$tests = LabTest::orderby('id', 'desc')->paginate(2);
return view('labTest.index', compact('tests'));
and this is view
{{ $tests->links() }}
Is the index named id if not in model put
protected $table = 'lab_test';
protected $primaryKey = 'whatever';
Where whatever is your index name.
So in the view, you see results, but pagination does not work, weird. Everything looks right. If you paginate 5 you see 5?
How many results are there here?
primary key name is 'id' so laravel will take care of that. Yep if I paginate 5 I see 5 results.
So I have 4 results in table and I am using paginate(2) and I see 2 results in view. When I change it to paginate(1) I see one result in view.
Thanks @jlrdw for your time. I will update task if I find some solution to it.
You could install laravel debugbar. Its a godsend and allows you to easily see the sql query that is performed on each page.
Paste ur controller/Model code here, so we can help you. Also do composer dump and php artisan cache clear, view clear
hey @manshu I am glad you want to help, But I have already pasted controller/model/view. Check my comment (https://laracasts.com/discuss/channels/laravel/laravel-pagination-links-not-working/replies/426319 )
Perhaps show the whole view code.
@jlrdw here is the whole view code
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="page-header">
<h1>All Tests</h1>
</div>
<p>Page {{ $tests->currentPage() }} of {{ $tests->lastPage() }}</p>
<ul class="list-group">
@foreach ($tests as $post)
<li class="list-group-item"><a href="{{ route('lab-tests.show', $post->id ) }}">{{ $post->test_name }}</a></li>
@endforeach
</ul>
<p>
{{ $tests->links() }}
</p>
</div>
</div>
@endsection
I just created a small table with test data and tried paging, it worked, but I don't use blade, I just did this:
<?php
foreach ($tests as $post){
echo $post->test_name;
echo '<br>';
}
echo $tests->links();
I used your query and model data.
Try replacing
<li class="list-group-item"><a href="{{ route('lab-tests.show', $post->id ) }}">{{ $post->test_name }}</a></li>
with
<li class="list-group-item">{{$post->id }}</li>
Just to see what happens, your query does work as expected. There has to be some problem in the view, start troubleshooting it.
Work on various areas of the view.
I am having the exact same issue right now. The simplest code I could write:
web.php
Route::get('test', 'GeneralController@foobar');
GeneralController.php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class GeneralController extends Controller {
public function foobar() {
$lala = DB::table('products')
->select('id')
->paginate(5);
return view("ho", compact('lala'));
}
}
ho.blade.php
{!! var_dump($lala) !!}
{{ $lala->links() }}
the output of "http://bidx.localhost/ho?page=8" is:
object(Illuminate\Pagination\LengthAwarePaginator)#475 (9) {
["total":protected]=>
int(223)
["lastPage":protected]=>
int(45)
["items":protected]=>
object(Illuminate\Support\Collection)#464 (1) {
["items":protected]=>
array(5) {
[0]=>
object(stdClass)#463 (1) {
["id"]=>
int(5)
}
[1]=>
object(stdClass)#91 (1) {
["id"]=>
int(6)
}
[2]=>
object(stdClass)#466 (1) {
["id"]=>
int(7)
}
[3]=>
object(stdClass)#467 (1) {
["id"]=>
int(8)
}
[4]=>
object(stdClass)#468 (1) {
["id"]=>
int(9)
}
}
}
["perPage":protected]=>
int(5)
["currentPage":protected]=>
int(1)
["path":protected]=>
string(24) "http://localhost/ho"
["query":protected]=>
array(0) {
}
["fragment":protected]=>
NULL
["pageName":protected]=>
string(4) "page"
}
<ul class="pagination">
<li class="disabled"><span>«</span></li>
<li class="active"><span>1</span></li>
<li><a href="http://localhost/ho?page=2">2</a></li>
<li><a href="http://localhost/ho?page=3">3</a></li>
<li><a href="http://localhost/ho?page=4">4</a></li>
<li><a href="http://localhost/ho?page=5">5</a></li>
<li><a href="http://localhost/ho?page=6">6</a></li>
<li><a href="http://localhost/ho?page=7">7</a></li>
<li><a href="http://localhost/ho?page=8">8</a></li>
<li class="disabled"><span>...</span></li>
<li><a href="http://localhost/ho?page=44">44</a></li>
<li><a href="http://localhost/ho?page=45">45</a></li>
<li><a href="http://localhost/ho?page=2" rel="next">»</a></li>
</ul>
See my reply above, it works. Has to be a view problem, I used OP's model and controller query. But I just did a simple view to test. So start simple and build around til the problem is identified.
I removed the return view(...) and just echoed $lala->links() in the controller itself, still the same issue, currentPage is always 1
I am using Laravel 5.4.36, but this discussion was the only one I found searching for this issue
I am 5.5 don't have exact revision in front of me.
You both do an update to the latest revision.
I haven't checked GitHub but I would check for issues, or consider updating to 5.5 at least.
Updated to the newest version of 5.6, still the same problem. Also happens with simplePaginate
@jlrdw I am using laravel 5.6.17
Okay start by building a simpler View and work up from there because in 5.5 it definitely works.
What do you mean by "building a simpler View"? I am not using any view right now or do I miss the point here? I am directly echoing $lala->links() in my controller so I am not using a view, right? :)
Please or to participate in this conversation.