lalit mehta
lalit mehta

Reputation: 49

how to pass get parameters in named routes in laravel

In my index.blade.php the code is as follows:

href="/finance/reports?type=monthly&year={{ $month['year'] }}&month={{ $month['id'] }}"

and in web.php file route is defined as:

Route::get('/reports', 'ReportsController@index')->name('reports');

How can I pass the parameters in index.blade.php to make it a named route.

Upvotes: 3

Views: 8736

Answers (3)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40919

Define the route as:

Route::get('/finance/reports/{type}/{year}/{month}')->name('reports');

and then use it from blade template the following way:

href="{{route('reports', ['type' => 'monthly', 'year' => $month['year'], 'month' => $month['id']])}}"

See the docs for more info: https://laravel.com/docs/5.6/routing#required-parameters

Upvotes: 2

Pinal Patel
Pinal Patel

Reputation: 157

Define the route as:

Route::get('/reports/{type}/{year}/{month}', 'ReportsController@index');

and use it in following way from the blade template

href="your_project_path/reports/monthly/{{ $month['year'] }}/{{ $month['id'] }}"

Upvotes: -1

lagbox
lagbox

Reputation: 50531

It is a named route already. To get a URL from the route helper for the named route with the query params appended:

route('reports', [
    'type' => 'monthly',
    'year' => $month['year'],
    'month' => $month['id'],
]);

Would be:

http://yoursite/finance/reports?type=monthly&year=WhatEverThatIs&month=WhatEverThatWas

I'm making assumptions about your routes and that the URI you used in the example is accurate.

Upvotes: 8

Related Questions