delete function Laravel 5.2 - php

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>

Related

How to submit a form using PUT http verb in Laravel

I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.
Now listing my form, my route and my exception.
{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
<input type="hidden" name="_method" value="PUT">
-
Route::resource('casas', 'CasasController');
exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:
With plain html / blade
<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
{{-- Your form fields go here --}}
<input type="submit" value="Update">
</form>
Wirth Laravel Collective it may look like
{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}
{{ Form::submit('Update') }}
{{ Form::close() }}
In both cases it's assumed that you pass a model instance $casa into your blade template
In your controller
class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}
public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}

Laravel calling #show instead of #destroy

I have a problem with my routes in Laravel (v. 4.2)..
View:
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
Route:
Route::resource('admin/users', 'App\Controllers\Admin\UserIndexController');
Controller:
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
When the button is clicked though it always prints out "show". Why is this?
HTML forms cannot make PUT, PATCH, or DELETE requests so you need to spoof it with Laravel.
Add this to your form...
<input type="hidden" name="_method" value="delete" />
I believe this generally isn't an issue with newer versions of Laravel since the Form builder has been removed from the core and is now being managed by LaravelCollective which will handle adding this input automatically.
https://laravel.com/docs/4.2/html#opening-a-form
Edited Answer:
Routes.php
Route::resource('admin/users', 'UserIndexController');
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
I am getting "destroy" as output.

How to solve MethodNotAllowedHttpException in RouteCollection.php line 218:?

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.

Laravel deleting a file NotFoundHttpException

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

Trying to delete multiple records at once in Laravel

I'm trying to make table with checkboxes where admin can check multiple products and delete them. So far I've made the form
#foreach($products as $product)
{{ Form::open() }}
<input type="checkbox" name="delete[]" value="{{ $product->product_id }}">
<a class="btn btn-primary" href="{{ URL::to('/admin/products/multiDdelete') }}?_token={{ csrf_token() }}">Delete</a>
{{ Form::close() }}
#endforeach
This is in my route
Route::get ('/admin/products/multiDdelete', ['uses' => 'AdminController#testDelete', 'before' => 'csrf|admin']);
And this in the controller
public function testDelete() {
$delete = Input::only('delete')['delete'];
$pDel = Product::where('product_id', $delete);
$pDel->delete();
return Redirect::to('/admin/test')->with('message', 'Product(s) deleted.');
}
So far when I check products and hit Delete page reload and I get Product(s) deleted but products aren't deleted. I think the problem is in how I pass ID's.. but I can't figured it out.
Your query isn't returning anything useful here. Even with ->get(), it would return a collection, which you can't use the way you want. You can add delete to your query instead:
Product::whereIn('product_id', $delete)->delete();

Categories