Route parameter passing wrong value to controller

65 views Asked by At

I have 2 links on the same view where I'm calling the same route with different parameters:

<a href="{{ route('getalladdress', ['addressType' => 'customer']) }}">Customer Address</a>
<a href="{{ route('getalladdress', ['addressType' => 'tax']) }}">Tax Address</a>

Route:

Route::get('getalladdress/{addressType}', 'AddressController@getalladdress');

Controller:

public function getalladdress($addressType) {
        //some code
return view('partials.searchaddress')
            ->with('addressType', $addressType);
    }

When I'm clicking on the first link, the addressType parameter is returning correctly, i.e., "customer". When I'm clicking on the second link, the addressType parameter is returned as the first one "customer" instead of "tax". I don't know what I'm doing wrong here. Any help would be appreciated. Thanks!

PS: I'm a newbie to Laravel.

1

There are 1 answers

1
fubar On BEST ANSWER

To use the route() helper function, you need to give your routes names.

Route::get('getalladdress/{addressType}', ['as' => 'getalladdress', 'uses' => 'AddressController@getalladdress']);

Then you can use the route name with the helper as follows:

<a href="{{ route('getalladdress', ['addressType' => 'customer']) }}">Customer Address</a>