Laravel - HasMany CRUD - Update - laravel

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

Related

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 !

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

Update / post database colum in Laravel

I have a general question.
I have a search form in larvel which returns results form the database.
in these i have an input field to enter a price if price is == 0
what my problem is when i enter price and submit it returns to the search page without my previous search results i.e it doesn't refresh the same page with results and the newly updated field etc.
form in view
{{ Form::open(['action' => 'price_input'])->with($gyms) }}
{{ Form::text('enter_price', null, ['class' => 'form-control', 'size' => '50', 'id' => 'enter_price', 'autocomplete' => 'on', 'runat' => 'server', 'required' => 'required', 'placeholder' => 'enter price!', 'style' => 'margin-bottom: 0px!important;']) }}
{{ Form::submit('Search', ['class' => 'btn btn- primary', 'style' => 'margin-left: 10px;']) }}
{{ Form::close() }}
route
Route::post('/', [ //not used yet
'as' => 'price_input',
'uses' => 'PagesController#priceUpdate'
]);
Model
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
return Redirect::back()->withInput();
}
Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gym);
}
not bothering with model as that works fine.
any ideas guys?
Thanks for your answer,
i have changed my controller to this
public function priceUpdate($gyms)
{
if (Input::has('enter_price'))
{
$price = Input::get('enter_price');
Gym::updatePrice($price);
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
}
but when i run it i get
Missing argument 1 for PagesController::priceUpdate()
with the $gyms being passed into the method.
if i take out the $gyms that goes away but not sure if its still being passed with session or not, sorry im a novice.
orignally i had a search box which when run returns
return View::make('pages.home')->with($data);
what is the difference between that and
return View::make('pages.home')->with($data);
when i do the above line it returns to the search page with no search options from before update the form, any ideas?
Currently, you are just retrieving an existing session and doing nothing with it. You need to do:
$gyms = Session::get('gyms');
return Redirect::to('pages.home') ->with('gyms', $gyms);
Or
return Redirect::to('pages.home')->with('gyms', Session::get('gyms'));
Then you can access the gyms in the view with $gyms.
Alternatively, you could access Session::get('gyms') in the view as well.
Also, not sure if it's just the way you pasted it here, but you have an unnecessary space before the ->with. Just wanted to make sure that's not part of the issue, too!

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.

form model binding and file upload

I got an answer previously to my question about how to create edit/update details form in laravel, and the answer was utilizing Form::model(modelName). Nevertheless, the page which is updating is the same page handling the insertion. I have the following form opening:
{{ Form::open(array('action' => 'SomeController#basic','files' => true)) }}
how can I change this to use the form::model instead? I also have an upload of file in same page (profile pic).
Laravel 4 or 5 Form <-> Model binding
Maybe is a "bit" late for the original poster, but for those who search for a solution, this can be used as a later reference.
The L4 solution is:
{{ Form::open($object, array('method' => 'PATCH', 'action' => array('SomeController#update', $object->id),'files' => true)) }}
For Laravel 5, you would use:
{!! Form::model($object, ['method' => 'PATCH', 'action' => ['ObjectsController#update', $object->id], 'files' => true]) !!}
Home this helps!
Form model binding can be done by declaring form open as
{{ Form::model($user, array('action' => 'SomeController#post', 'files'=> true)) }}
You pass the model to the view for example here $user is the model.
$user = User::find(1);
return View::make('editUser')->with(compact('user'));
For file upload, if the $user is isset you could display the image. Along with the upload button. When update is clicked you can check
if(Input::has('file'))
then move the file and upload the path in the database. May be there is a different solution for this.
Is this what you were looking for?

Resources