0
votes

I want to pass parameter using ? in url, like

http://example.com/somthing?name=myname&phone=12121212

when i add a '?' in Route file it shows 404

In my route file

Route::get('test?id={id}&name={name}','TestController@test')->name('frontend.test');

My controller

public function test($id, $name)
{
    
    dd($name);
}

But when i use it like this works fine

Route File:

Route::get('test/id={id}&name={name}','TestController@test')->name('frontend.test');

Controller:

public function test(Request $request)
{
    
    dd($request->route()->parameters());
}

i have a project with raw php i want to convert it to Laravel, so i don't want to change the url structure.

how can i achieve this in Laravel route.

1
it might be similar to this: stackoverflow.com/questions/38737019/…Steven
Thanks, i got it. actually i didn't find how to get those parameter, now its working.Basher

1 Answers

1
votes

I wouldn't define the parameters within the route.

Route::get('test','TestController@test')->name('frontend.test');

I prefer to validate and get the request parameters like this:

use Illuminate\Http\Request;

...

public function test(Request $request)
{
    $params = $request->validate([
        'id' => 'required|numeric',
        'name' => 'required|alpha'
    ]);

    dd($params['id'], $params['name']);
}