Yorkata's avatar

Pagination not working

Hello! I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.

In my Controller I added this $competitions = Competition::latest()->paginate(1);

Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();

And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.

Can you please tell me what I did wrong? Any and all help gratefully received.

0 likes
15 replies
Sinnbeck's avatar

Everything looks correct. What does the url change from/to when you click the next page button?

1 like
Sinnbeck's avatar

@Yorkata And you have more than 1 item in the database? Or are all created at the same time perhaps?

$competitions = Competition::latest()->orderBy('id')->paginate(1);
Yorkata's avatar

@Sinnbeck Not at the same time. I've got 4 items in total if I put ->paginate(1) it shows only 1 at a page, if I put ->paginate(2) it shows 2 etc etc

Sinnbeck's avatar

@Yorkata so my updated query gives you the same? Can you show the competition model?

Yorkata's avatar

@Sinnbeck my model

class Competition extends Model
{
    use HasFactory;

    use SoftDeletes;
    
    use RevisionableTrait;
    protected $revisionCreationsEnabled = true;

    protected $fillable = ['name', 'frequency', 'status', 'mode', 'type'];

    /**
    * Get the events for competition
    */
    public function events() {
      return $this->hasMany(Event::class, 'competition_id', 'id'); 
    }
}
Yorkata's avatar

My route if that may help

//competitions
use App\Http\Controllers\CompetitionController;
Route::get('competitions/{competition}/exports', [App\Http\Controllers\CompetitionController::class, 'exports'])->name('competitions.exports');
Route::get('competitions/{competition}/generate_export/{type}', [App\Http\Controllers\CompetitionController::class, 'generate_export'])->name('competitions.generate_export');
Route::resource('competitions', CompetitionController::class);
Sinnbeck's avatar

@Yorkata Is the project public on github so I can inspect it? I cannot recreate the problem at all

Yorkata's avatar

@Sinnbeck Sadly it's not and I cannot upload it. I'll continue searching for the annoying problem

Sinnbeck's avatar

@Yorkata If you make a new project and copy over the model + migration for the table + controller + view, can you recreate it?

Yorkata's avatar

Any other suggestions? I even tried to edit the .htaccess file but nothing helped

Yorkata's avatar

@sinnbeck I've been thinking could this be a routing problem? When I click on a new page the URL changes so the {{ $competitions->links() }} actually work, correct? Should I add a param or something in the Route::get? Thank you for your time and ideas! I am attaching my routes and controller function again:

// in the controller 
  public function index() 
    {   
       
        $competitions = Competition::orderBy('id', 'DESC')->paginate(2);
        return view('competition.index', compact('competitions')); 

    }
//competitions
use App\Http\Controllers\CompetitionController;
Route::get('competitions/{competition}/exports', [App\Http\Controllers\CompetitionController::class, 'exports'])->name('competitions.exports');
Route::get('competitions/{competition}/generate_export/{type}', [App\Http\Controllers\CompetitionController::class, 'generate_export'])->name('competitions.generate_export');
Route::resource('competitions', CompetitionController::class);

Please or to participate in this conversation.