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

FounderStartup's avatar

Correct way to redirect() with route and arguments

My route is

Route::get('/responder/conversation/{listingId}/{responderId}', [MessagesController::class, 'responderconversation'])->name('responder.conversation');

Any my present return code. in the controller is

        return view('frontend.messaging.conversation',compact('responder','listing','messages','user'));

I need to redirect to the route of the page with arguments else its storing the data twice. What will be the correct code for redirect () ?

0 likes
10 replies
FounderStartup's avatar

I tried this but its giving error@webrobert

return redirect()->route('responder.conversation', [$listingId, $responderId],compact('responder','listing','messages','user'));

webrobert's avatar
Level 51

@FounderStartup

The second parameter of route should be an array in your example. Like this…

 … route('profile', ['id' => 1, 'photos' => 'yes']);

1 like
FounderStartup's avatar

error I am getting@webrobert

Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, array given, called in /Applications/XAMPP/xamppfiles/htdocs/buzzinghouses/vendor/laravel/framework/src/Illuminate/Routing/Redirector.php on line 233

webrobert's avatar

@founderstartup and here is part 2 on

##preference.

    // as mentioned...
    return redirect()->route('profile');
    // but this also works...
    return redirect(route('profile'));
    // or 
    return redirect()->to(route('profile'));
    // and as you mentioned
    return redirect()->back() // if youre going back

and, I just found recently...

    return redirect()->away('https://google.com') // away cleans up the headers

Personally for named routes lately I use this one...

    // i like that it's expressive even though its slightly longer
    return redirect()->to(route('profile')); 

##For the parameters "arguments" part...

function route($name, $parameters = [], $absolute = true)

Depending on the $name (here we've suggested using named routes) you can...

   // for a single parameter pass the expected binding like the id.
   route('profile', 1)
  // or for say a bound slug
   route('profile', "username")
   // be explicit and pass the id with another parameter
   // that NOT expected that becomes avaible in request 
   route('profile', ['id' => 1, 'photos' => 'yes']);
   // and as Snapey mentioned, pass the expected models and laravel will bind them
   route('responder.conversation', [$listing, $responder]);
1 like
Snapey's avatar

it should be

redirect()->route('responder.conversation', [$listing, $responder]);
1 like

Please or to participate in this conversation.