Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4: call a route function from controller with parameters

I want to call a route function from my controller with some parameters.

This is my Controller

public function myFunction($id, $name)
{
    $id = 1;
    $name = 'john';

    return redirect()->route('details/' . $id . '/' . $name);
}

This is my route

Route::get('details/{id}/{name}',['uses' =>'My_controller@myFunction']);

This is the error I get when I run the script.

InvalidArgumentException in UrlGenerator.php line 304:

Route [details/1/john] not defined.

Please Help

like image 452
Ajmal Razeel Avatar asked Feb 19 '26 10:02

Ajmal Razeel


1 Answers

route() helper requires you to pass route name and parameters:

function route($name, $parameters = [], $absolute = true)
{
    return app('url')->route($name, $parameters, $absolute);
}

So you need to give your route a name:

Route::get('details/{id}/{name}',['uses' =>'My_controller@myFunction'])->name('details');

..and then you can pass parameters in an array:

return redirect()->route('details', array('id' => $id, 'name' => $name));

Hope this helps

like image 103
Sami Samiuddin Avatar answered Feb 21 '26 00:02

Sami Samiuddin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!