Laravel 4: pointing a form to a controller function - laravel

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.

Related

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]) }}

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

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);
}

How to include Laravel Controller file like blade

First my master.blade file include menu bar using this
Include Blade file menu.blade.php
#include('menu')
But Finally I realize to send some data from db to menubar, then I create controller, Controller name is MenuController, then I create route "admin-menu". Now I want to include that link to my master blade. how to do that thank you,
To pass data to a view from either a route closure or a controller you do one of the following:
$now = \Carbon\Carbon::now();
View::make('my-view', ['name' => 'Alex', 'date' => $now]); // pass data into View:::make()
View::make('my-view')->with(['name' => 'Alex', 'date' => $now]); // pass data into View#with()
View::make('my-view')->withName('Alex')->withDate($now); // use fluent View#with()
So you'd just use them in the View::make() call as you presumably already are:
// in a route closure
Route::get('some-route', function () {
return View::make('menu', ['name' => 'Alex']);
});
// in a controller
public function someRoute()
{
return View::make('menu', ['name' => 'Alex']);
}
Interestingly, in a lot of frameworks/templating systems, if you wanted to include a partial, you'd pass the data you want to be available in that partial in the partial call, but Laravel doesn't quite do this. For example in a made-up system you may have something like this:
// in controller:
$this->render('home', ['name' => 'Alex', 'age' => 30]);
// home.php
<?php echo $name; ?>
<?php echo $this->partial('home-age', ['age' => $age]); ?>
// home-age.php
<?php echo $age; ?>
But in Laravel, all current view variables are automatically included into partials for you. Now I tend to like to specify the variables anyway (Blade does allow you to do this as above), and obviously it can be used to override a view variable:
// route:
return View::make('home', ['name' => 'Alex', 'age' => 30, 'gender' => 'male']);
// home.blade.php
{{ $name }}
#include('home-extra', ['age' => 20])
// home-extra.blade.php
{{ $age }}
{{ $gender }}
The above code would output:
Alex
20
male
So the age is overridden in the #include, but the un-overridden gender is just passed along. Hopefully that makes sense.

Get the model ID when updating a resource

I have a form that will submit a Patch request to the controller's update method.
But the update method requires to have $id, as you can see below whenever I try that I get a
No query results for model [Item]. Since the update method did not receive the $id of the model
public function update($id)
{
$item = Item::findOrFail($id);
$update = Input::all();
// some codes to save changes
return Redirect::route('items.index');
}
Another thing is that whenever I submit the form, url turns into something like this:
mw.dev/items/%7Bitems%7D
Edit
routes.php
Route::resource('items','ItemsController');
ItemController
public function edit($id)
{
$item = Item::findOrFail($id);
return View::make('items.edit')->with('item',$item);
}
I have included the code on my edit.blade.php
{{Form::open(array('route' => 'items.update', 'method'=>'patch'))}}
{{Form::text('barcode', $item->barcode, array('placeholder' => 'barcode'))}}
{{Form::text('imei',$item->imei, array('placeholder' => 'imei'))}}
{{Form::text('item_name', $item->item_name, array('placeholder' => 'item name'))}}
{{Form::submit('edit')}}
{{Form::close()}}
You have to pass the model to your view and need to pass the id parameter when generating the form. Assume that you have a User model and it's available in the view. So you may generate the form action using something like this:
// Using Form::open method with route
Form::open(array('route' => array('route.name', $user->id)))
// Using Form::model method with route
Form::model($user, array('route' => array('route.name', $user->id), 'method' => 'patch'))
// Using Form::open method with action
Form::open(array('action' => array('Controller#update', $user->id), 'method' => 'patch'))
// Using Form::open method with url
Form::open(array('url' => 'something/update/' . $user->id, 'method' => 'patch'))
Check more on Opening A Form.
The URL
mw.dev/items/%7Bitems%7D
is most likely the url-encoded form of
mw.dev/items/{items}
I suppose there is a problem in the form submission or in the <form>'s action paremeter or in the Route::* declaration in routes.php.
This could also explain why you don't get any $id upon submission.

Resources