this is my form in my view
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
controller in which i delete file from database and the original file
public function destroyFile($file_name, $id)
{
File::findOrFail($id)->delete();
$file_path = storage_path('documents').'/'.$file_name;
$destinationPath = $file_path; File::delete($file_path);
return redirect('/documents');
}
This is the route
Route::delete('documents/{file}/{id}','FilesController#destroyFile');
And when i press submit button I get NotFoundHttpException
Try to use this
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name, $file->id] ]) !!}
Actually, their answers are correct. You need the _method to be DELETE. When I am using this. Laravel do it for me.
Or you can put this on your form
<input type="hidden" name="_method" value="DELETE">
or
{!! Form::hidden('_method', 'DELETE') !!}
It is not possible to use this method with html forms in most browsers, most only support GET and POST.
So the reason for this request not working is because the browser sends this as a GET request, wich is the default.
GET, POST, PUT and DELETE are however supported in most major browsers when using XMLHttpRequests (ajax).
add {{ method_field('DELETE') }} to your form .
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{{ method_field('DELETE') }}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
The reason is that HTML forms does not support PUT, PATCH, DELETE actions. Basically you need to spoof them as described here. https://laravel.com/docs/5.2/routing#form-method-spoofing
Related
{!! Form::open(['action'=>['PostsController#update',$post->id],'method'=>'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title', $post->title,['class'=>'form-
control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body', $post->body,['id'=>'article
ckeditor','class'=>'form-control','placeholder'=>'Body Text'])}}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Update',['class'=>'btn btn-success'])}}
{!! Form::close() !!}
I am using this {{ Form::hidden('_method','PUT') }} to update my post because there is no other way. Is there any better way or not?
Here is my controller (postcontroller):
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required',
'body' => 'required'
]);
$post = Post::find($id);
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/posts')->with('success','Post Updated Sucessfully');
}
This is the default and correct way to use UPDATE form. You have to use POST in form method and PUT as request parameter (hidden) with form.
What you missed here is CSRF token in form, which can be used in multiple ways like:
{!! Form::token() !!}
{!! Form::hidden('_token', Session::token()) !!}
{!! csrf_field() !!}
#csrf <!-- since Laravel 5.6 -->
Similarly, you can use PUT as:
#method('PUT')
I have an edit view and i am using a partial _form view.
Is there a way to check if the form is a patch or post?
What i plan to do is to change the hidden field in edit form
#if (form is post)
{!! Form::hidden('signature') !!}
#else
<div class="form-group">
{!! Form::label('signature', 'Signature: ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('signature', null, ['class' => 'col-md-2 form-control', 'required']) !!}
</div>
</div>
#endif
because this variable is already saved to DB and i want to load it for edit.
Or to check if form is post, that would work also!
I usually pass the variable to a view where I set action, like:
$action = 'store';
Then I use this variable to build route name:
{!! Form::open(['route' => 'post'.$action, ....
And detect what type of action is needed:
#if ($action == 'store')
I guess it's the most readable and simple way to achieve what you're trying to achieve. You can do something similar.
Try this:
$isPut= Request::isMethod('put');
if($isPut) {
//
}
My view is like this :
#foreach($users as $user)
<tr>
<td>{!! $user->id !!}</td>
<td>{!! $user->username !!}</td>
<td>{!! $user->phone !!}</td>
<td>{!! $user->address !!}</td>
<td>
{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
<div class='btn-group'>
</i>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes\web.php is like this :
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
Route::resource('users', 'UserController');
My controller is like this :
public function destroy($id, $year)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index.year', ['year' => $year]));
}
$this->userRepository->delete($id);
Flash::success('User deleted successfully.');
return redirect(route('users.index.year', ['year' => $year]));
}
There is exist error like this :
MethodNotAllowedHttpException in RouteCollection.php line 218:
And the url looks like this : http://localhost/mysystem/public/users/2?2016
When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2016
Is there any people who can help me?
In your view you have declared the form method as DELETE; but in the routes\web.php file, you have declared the route as GET
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'get']
)!!}
Your routes/web.php should look like so,
Route::get('users/index/{year}', 'UserController#index')
->name('users.index.year');
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Route::resource('users', 'UserController', ['except' => ['index', 'destroy']]);
Keep your controller code as it is.
This should do the trick.
Just a suggestion
It's better not to keep delete operation on GET request. So change the DELETE route like so
Route::delete('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'delete']
)!!}
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
id is not declared in route and may be this is the cause, So change you route:
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
to this
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')->name('users.destroy.year');
Hope this solve this cause.
I've been learning laravel 5.2 recently, and i've made a delete function which should delete records from my database but instead of deleteing the records it's adding a blank row into my database
This is the Route im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'destroy', 'edit', 'update', 'create']]);
This is the controller function i use for it
public function destroy(request $request , product $product)
{
$product->delete();
return redirect(Route('producten.index'));
}
This is the form i've made for it.
{{ Form::Open(['Route' => 'producten.destroy', $product], ['method' => 'delete']) }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
when i viewed the source-code it said it was using a POST method instead of a delete method, and also when i add($product) i got a blank page, also i found out that when i hit the submit button it goes to the store method i've made and i dont know why,
if u need more information just let me know and i'll add it in the question
route and method should be in the same array, not in two differents arrays.
{{ Form::Open(['method' => 'DELETE', 'route' => ['producten.destroy', $product]]) }}
{{ method_field('DELETE') }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
I think you have something wrong with form. Can you try with this:
<form action="{{ route('producten.destroy', ['product' => $product->id]) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Remove</button>
</form>
I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project, $document->id]
]) !!}
{!! $documentData->documentData !!}
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
$documentData->documentData contains the html code for this particular form.
Now my problem is, $documentData->form_data contains the old inputs for this form.
Is there any way to get this old input into the form, the way I am currently handling things?
Thanks
in controller you can access old input by $request->flash(); while in frontend you can access old by input type="text" name="name" value="{{ $name }}"