Laravel pagination problem when deleting last item

1132 0 0 0

Last Updated : 2024-07-16 16:41:47

when deleting the last record on pagination page , this will make that page be fully empty with no records ... this snippet will allow you to redirect to the main route when deleting the last record on pagination and the page is empty

just add this line in your controller

$messages = Message::where('type', 'received')->orderby('id', 'desc')->paginate(2);

if (request()->get('page') > 1 && $messages->isEmpty()) {
return redirect()->route('main route here');
// change $messages to any records name

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.