Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CamKem's avatar
Level 10

Redirects with Query String Laravel

Is it really necessary to write logic for handling query string on redirects(), or does Laravel have a built in method for handling the query strings in redirects?

From memory, in my last project I didn't need to worry about this, it just worked fine, so I am not sure whats going on...

This is what I am working with to make sure that when someone submits a message, the store method redirects to the correct page:

        $limit = self::PAGINATION_LIMIT;
        $count = Chat::query()->where('user_id', auth()->id())->count();
        if ($count > $limit) {
            return redirect()->route('chat.index', [
                'page' => $count / $limit
            ]);
        } else {
            return redirect()->route('chat.index');
        }
0 likes
1 reply
CamKem's avatar
CamKem
OP
Best Answer
Level 10

Turns out this is what you need to do, there is no magic solution in Laravel:

        $limit = self::PAGINATION_LIMIT;
        $count = Chat::query()->where('user_id', auth()->id())->count();
        if ($count > $limit) {
            return redirect()->route('chat.index', [
                'page' => ceil($count / $limit),
            ]);
        } else {
            return redirect()->route('chat.index');
        }

Please or to participate in this conversation.