My Form in the edit view:
<form class="form-horizontal" role="form" method="PUT" action="{{ route('locations.update', $location->id) }}">
{{ csrf_field() }}
// All form Fields ...
</form>
My routs for this case:
| GET|HEAD | locations/create | locations.create | App\Http\Controllers\LocationController#create
| PUT|PATCH | locations/{location} | locations.update | App\Http\Controllers\LocationController#update
| GET|HEAD | locations/{location} | locations.show | App\Http\Controllers\LocationController#show
| DELETE | locations/{location} | locations.destroy | App\Http\Controllers\LocationController#destroy
My update method in the locations controller
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
dd($request);
}
The result on form submitting
the dd($request); result is not showing up.
Any hints for me what i am doing wrong here?
Thanks a lot!
Web browsers don't support PUT routes, only GET and POST. To solve this, you can use Form Method Spoofing By adding a hidden field to your form. Like so:
<form class="form-horizontal" role="form" method="post" action="{{ route('locations.update', $location->id) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
// All form Fields ...
</form>
Related
i want to update a customer using patch methodn here is my route
Route::patch('/customers/updateCustomer', 'CustomerController#update')
->name('customers.update');
and here is my form :
<form method="POST" action="{{ route('customers.update') }}">
#csrf
#method('PATCH')
how to pass the id of the customer , the update method requires two parameters, when the first one is the data from the from , the second should be the id of the customer
You should send the customer id with the route() helper function.
Make sure to send the customer object during the form rendering.
<form method="POST" action="{{ route('customers.update', $customer['id']) }}">
#csrf
#method('PATCH')
And modify the route slightly.
Route::patch('/customers/updateCustomer/{customerId}', 'CustomerController#update')->name('customers.update');
add customer id to the route like
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
and then your form action should be like
<form method="POST" action="{{ route('customers.update',$customer->id) }}">
you have to send the customer object from where you are returning to the form.
and finally your update function
public function update(Request $request, $id)
{
//your things to do
}
You have to use like this
<form method="POST" action="{{ route('customers.update',['id'=>$customer->id]) }}">
And your route should be
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
And your controller should be
public function update(Request $request, $id){
//your code
}
I have developed a CRUD application in laravel 5.5
All methods works fine in local laravel (I mean with the "simulator" started by command php artisa serve), but after the deploy of the application in xampp the method store in my controller stop to works(just this)!
In my web.php I added this route:
Route::resource('example','exampleControllerView');
the result of the command php artisan:
POST | example | example.store | App\Http\Controllers\exampleControllerView#store
| | GET|HEAD | example | example.index | App\Http\Controllers\exampleControllerView#index
| | GET|HEAD | example/create | example.create | App\Http\Controllers\exampleControllerView#create
| | GET|HEAD | example/{example} | example.show | App\Http\Controllers\exampleControllerView#show
| | PUT|PATCH | example/{example} | example.update | App\Http\Controllers\exampleControllerView#update
| | DELETE | example/{example} | example.destroy | App\Http\Controllers\exampleControllerView#destroy
| | GET|HEAD | example/{example}/edit | example.edit | App\Http\Controllers\exampleControllerView#edit
this is the method store in my controller
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:4',
'description'=> 'required',
]);
$example = example::create(['name' => $request->name,'description' => $request->description]);
return redirect('/example/'.$example->id);
}
This is the view create (url : http://localhost/project/public/example/create):
<form action="/example" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="title">example nome</label>
<input type="text" class="form-control" id="taskTitle" name="name">
</div>
<div class="form-group">
<label for="description">example descrizione</label>
<input type="text" class="form-control" id="exampleDescription" name="description">
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button type="submit" class="btn btn-primary">Submit</button>
After the submit button I see in the new page this uri:
http://localhost/example
and in the page a 404 error: Object not found!
The requested URL does not exist on this server.
I thing that is a problem of routing and url but I don not the why in local all work fine...
thanks in advances
Change this line-
<form action="/example" method="post">
To this-
<form action="/project/public/example" method="post">
Better use Laravel Collective, this will automatically configure your base path and has a lot of other benefits like csrf protection.
Your code should work fine. It redirects a user to the show method with ID of created entity. Instead of using model binding, try to do this:
public function show($id)
{
dd('The show method was executed and ID is ' . $id);
....
}
I have this code here which is listing all items from one user, user and item are two differents model and they have relationship with their primary keys.
#foreach($user->items as $item)
<p class="heading">
{{ $item->item_id }}
</p>
#endforeach
My question is how to send that item_id with post request to controller, because when I write like this for example <form class="form-horizontal" method="post" action="{{ route('preview', $item->item_id) }}" role="form" enctype="multipart/form-data"> it gives me that $item variable is not defined.
My controller:
public function preview($item_id){
return view('itempreview')->with('item_id', $item_id);
}
Any ideas how to send that item_id?
You should use array as 2nd param to route() method!
You are doing it wrong, you should send it like this:
route('preview', ['item_id' => $item->item_id]);
Make sure, your route should be defined as this:
Route::post('preview/{item_id}', 'ControllerName#preview')->name('preview');
Hope this helps!
You need to send $item_id as part of the POST:
#foreach($items as $item)
<form method="post" action="{{ route('preview') }}">
<input type="hidden" name="item_id" value="{{ $item->item_id }}">
</form>
#endforeach
Then in the method that receives the POST:
public function index() {
$item_id = request()->input('item_id');
return view('preview');
}
Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.
On my route.php I have
Route::resource('users', 'UserController');
Artisan route command gives me
+--------+-----------------------------+---------------+------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-----------------------------+---------------+------------------------+----------------+---------------+
| | GET|HEAD / | | Closure | | |
| | GET|HEAD users | users.index | UserController#index | | |
| | GET|HEAD users/create | users.create | UserController#create | | |
| | POST users | users.store | UserController#store | | |
| | GET|HEAD users/{users} | users.show | UserController#show | | |
| | GET|HEAD users/{users}/edit | users.edit | UserController#edit | | |
| | PUT users/{users} | users.update | UserController#update | | |
| | PATCH users/{users} | | UserController#update | | |
| | DELETE users/{users} | users.destroy | UserController#destroy | | |
+--------+-----------------------------+---------------+------------------------+----------------+---------------+
Then I have a page for editing users with 2 forms, the first one for editing, the second one for deleting:
{{ Form::open(array('route' => array('users.update', $user->id), 'method' => 'put')) }}
...
{{ Form::open(array('route' => array('users.destroy', $user->id), 'method' => 'delete')) }}
Finally into the UserController I use a validation that redirect to the previous page in case of unsuccesful validation:
if (!$this->user->isValid($id))
{
return Redirect::back()->withInput()->withErrors($this->user->errors);
}
When I land on the edit page from the the users list page, the HTML for both forms looks fine (note the hidden _method field with value DELETE):
<form method="POST" action="http://www.virtualbox.me/users/8" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT">
...
<form method="POST" action="http://www.virtualbox.me/users/8" accept-charset="UTF-8"><input name="_method" type="hidden" value="DELETE">
But if I insert a value on a field that cause the validation to fail, then on the reloaded page also the second form get method PUT instead of DELETE:
<form method="POST" action="http://www.virtualbox.me/users/8" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT">
...
<form method="POST" action="http://www.virtualbox.me/users/8" accept-charset="UTF-8"><input name="_method" type="hidden" value="PUT">
Am I doing something wrong?
Something like this might work.... change this line to:
return Redirect::back()->withInput(Input::except('_method'))->withErrors($this->user->errors);
Actually Laravel automatically change your method behind the scene it is mentioned as a note in Docs. As browsers don't understand PUT, PATCH, DELETE requests.
So you need to add the _method field explicitly in your DELETE form.
<field type="hidden" name="_method" value="DELETE">