Passing a param containing a path to a controller - laravel

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

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

Laravel - HasMany CRUD - Update

I need some help, please,
I have two tables: carriers and sell
Carrier CRUD is working. Sell CRUD is also working, except Update method.
In order to update, i need carrierId and sell id, right?
This is my CarrierSellController:
And this is my carriers/sell/edit.blade.php
The model binding is working:
Form action has /edit at the end of the url. It's oke?
If i hit submit button, i get this error:
Try this one
{!! Form::model($sell,['url'=>url('carriers/'.$sell->carrier->id.'/sell/'.$sell->id),'method'=>'patch']) !!}
and in the Sell model add
public function carrier()
{
return $this->belongsTo(Carrier::class);
}
make sure the method is PUT
add this to your form
{{ method_field('put') }}
Why don't you if you want to be absolutely sure just try declaring the route yourself like:
Route::put('carriers/{carrierId}/sell/{id}', [ 'as' => 'carriers.sell.update', 'uses' => 'CarrierSellController#update' ]);
And then in your view have this:
{!! Form::model($sell, ['method' => 'PUT', 'route' => ['carriers.sell.update', 'carrierId' => $sell->carrier->id, 'id' => $sell->id]) !!}

Controller Method not getting called

I am using Laravel 5.0
I have a method in MyController
public function myMethod($id) {
dd($id);
}
The routes.php file
Route::post('path1/{obj-id}/path2', 'MyController#myMethod');
Route::resource('path1', 'MyController');
In the view file, I am calling the method through a form on submit
{!! Form::open(['action' => ['MyController#myMethod', $myObject->id]]) !!}
Now the problem is, every time I click on Submit I get a 404 error. The URL in the address bar changes to path1/1/path2 as expected, but I get 404.
Where am I going wrong?
I got the solution myself.
Turns out one cannot have a dash (-) inside {} in one's routes.
My route in routes.php was initially
Route::post('path1/{obj-id}/path2', 'MyController#myMethod');
I changed it to
Route::post('path1/{id}/path2', 'MyController#myMethod');
and now everything works fine.
Sorry for missing the - in the original question. Thank you all who tried to help.
You can give your route a name and use this name in form
Route::post('path1/{id}/path2', [
'as' => 'myroute', 'uses' => 'MyController#myMethod'
]);
Now use it at your form like this
Form::open(array('route' => array('myroute', $myObject->id)))
Instead of using {!! Form::open(['action' => ['MyController#myMethod', $myObject->id]]) !!}, you need to use any of these:
{!! Form::open(['url' => 'path1/'.$myObject->id.'/path2']) !!}
Or, if you use Name routes i.e. mymethod.update, you can do it easily with that:
{!! Form::open(['routes' => ['mymethod.update', $myObject->id]]) !!}
BTW, if you really want to use 'action', you need to change your url like this in your routes.php file:
Route::post('path1/path2/{id}', 'MyController#myMethod');
Route::resource('path1', 'MyController');
Hope this help!
The Problem is with your route .
Route::post('path1/{id}', 'MyController#myMethod');
And in View, You missed the Post method . Change your view to below
{!! Form::open(['action' => ['MyController#myMethod', $myObject->id, 'method' => 'post']]) !!}
Hope this helps !

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.

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