how to pass get parameters in named routes in laravel - 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.

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.

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

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'] }}"

Related

Laravel #extends second parameter array explanation

Question:
I am new to laravel,
#extends('layouts.app', ['page' => 'Receipts', 'pageSlug' => 'receipts', 'section' => 'inventory'])
I understand #extends first parameter 'layouts.app', but what does the second parameter do?
In Laravel #extend second parameter just to pass data in another blade.
#extends('layouts.app', ['title' => 'Page Title'])
This is how the second parameter works in laravel main layout
<title>App Name - {{ ucfirst($title) }}</title>
Since you are extending layouts.blade.php file, you have to look into the file. In layouts.blade.php you need variables such as $page, $pageSlug, $section to be defined. So you are defining/passing each variable.
Like Python
page = Receipts, pageSlug = receipts, section = inventory
equals
'page' => 'Receipts', 'pageSlug' => 'receipts', 'section' => 'inventory'

Add locale to laravel resource route

I have following route that expect locale in prefix
Route::group(['prefix' => '{locale}/admin', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function() {
Route::resource('/profile', 'Admin\ProfileController', ['except' => ['edit', 'create', 'destroy', 'show', 'store']]);
});
The final route supposed to be like this:
www.example.com/en/admin/profile
and
www.example.com/en/admin/profile/1
while I can get index route www.example.com/en/admin/profile, but I cannot get update route www.example.com/en/admin/profile/1 instead I have this
www.example.com/1/admin/profile/en
Here is how my routes are defined in blade
// Return index route correctly
{{route('profile.index', app()->getLocale())}}
//And
{{route('profile.update', [$user->id, app()->getLocale()])}}
// Return update route wrong as I mentioned above.
My question is: How should I define my update route to return correct url?
You are passing your parameters in wrong order. You need to send locale first and $user->id after that.
{{ route('profile.update', [app()->getLocale(), $user->id]) }}

LARAVEL: POST and GET routes with same name scrambling link_to_route

I got these routes:
Route::get('prefix/list/{page?}/{size?}', ['as' => 'prefix.list', 'uses' => 'MyController#getList']);
Route::post('prefix/list', ['as' => 'prefix.list', 'uses' => 'MyController#postList']);
When I call link_to_route() like so:
{{ link_to_route('prefix.list', $page, ['page' => $page, 'size' => $size]) }}
It creates this link:
http://my.site/prefix/list?page=5&size=12
But when I remove the post route, it renders correctly this:
http://my.site/prefix/list/5/12
I don't want to change the name of the routes because my system depends on them being the same. How can I solve this?
You could try just changing the order of the routes in your routes file, so that the get one comes last and overrides the post for the purposes of link_to_route().

Passing a param containing a path to a controller

I'm trying to pass a variable who contains a path from a form to a controller function in Laravel 4, with the purpose of download an image. I tried a lot of things but nothing worked for me. If I don't pass the parameter in the route, I get a missing parameter error, and if I pass the parameter in the route, I get a NotFoundHttpException.
Here's my code:
View Form:
{{ Form::open(array('route' => array('download', $myPath))) }}
{{ Form::submit('Download', array('class' => 'generator')); }}
{{ Form::close() }}
Route:
Route::post('/download/{myPath}', array('uses' => 'ImagesController#download', 'as' => 'download'));
ImagesController function:
public function download($myPath){
return Response::download($myPath);
}
When I click the submit I'm obtaining a URL like this with NotFoundHttpException:
http://localhost/resizer/public/download/images/myimage.jpg
I don't understand what I'm missing.
You are actually posting two variables. Try this instead
Route::post('/download/{myFolder}/{myFile}', array('uses' => 'ImagesController#download', 'as' => 'download'));
and in your controller
public function download($myFolder, $myFile){
return Response::download($myFolder.'/'.$myFile);
}

Laravel 4: pointing a form to a controller function

I can't understand, how to set up the Form action to direct to a function of a specific controller.
This is my blade code:
{{ Form::open(array('route'=>'user.search')) }}
But I get this error :
Unable to generate a URL for the named route "user.search" as such route does not exist.
the controller (UserController) has a function with this prototype
public function search(){ ... }
I have also tried to set up a route like this in route.php
Route::post('user/search', 'UserController#search');
What is wrong with this code?
You can do it like
{{ Form::open( array('url' => URL::to('user/search')) ) }}
Because you don't have a name for the route. To define a name for the route, use following syntax,
Route::post('user/search', array( 'as' => 'userSearch', 'uses' => 'UserController#search' ));
So, you can use the route by it's name, as
{{ Form::open( array('route' => 'userSearch') ) }} // 'search' method will be invoked
Also, you can directly use the action of a controller as
{{ Form::open( array('action' => 'UserController#search') ) }}
Check Routing and Form.

Resources