laravel delete method not working - laravel

I've made a small laravel project but the delete method is nog working:
I use a resource controller
my route is :
Route::resource('roles','Admin\RoleController');
in my view I have
<form action="{{route('roles.destroy',$role->id)}}" style="display:inline">
#method('delete')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
But when i click the button it will show me the role ( = get method of the resource )
What am I doing wrong ?

If you're using Laravel 5.1 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
If you're using Laravel 5.6 or later
<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
You can read more about method spoofing in Laravel Documentation.

Check Laravel's documentation.
Did you try adding method="POST" to the form?

Related

Laravel | Form not submitting to route given in action [SOLVED]

I'm trying to submit a form to the right route using the action:
<form action="{{ route('document.destroy', $d->id) }}" method="POST" style="display: inline;">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<span type="text" value="" class="btn btn-success" readonly="readonly">
{{ $d->file_name }} ({{ $d->file_size }}) <i class="fa fa-times"></i>
</span>
<button class="btn btn-xs btn-default" type="submit" data-toggle="tooltip" title="Verwijder" onclick="return confirm('Weet je zeker dat je dit document wilt verwijderen')"><i class="fa fa-times"></i></button>
</form>
But yet it links to a different controller. What am I doing wrong?
I also specified the controller in the routes file:
Route::resource('document', 'DocumentController');
To add more context: this view gets passed from a different controller than I want to use for the DELETE function.
EDIT
I used a form inside another form, my stupid head didn't see it.
for deleting you can try this
<form action="{{action('DocumentController#destroy', $d->id)}}" method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
and Destroy method
public function destroy($id)
{
$doc= Document::find($id);
$doc->delete();
return redirect('/home')->with('success', 'Document has been deleted!!');
}

laravel Route::resource not working for delete

Laravel Route::resource('countries', 'CountriesController'); now working for DELETE
Working only as (separately)Route::delete('/countries/{country}/delete', 'CountriesController#destroy');
<div class="row">
<div class="col-12">
<h1>Details for {{ $country->countryName }}</h1>
<p>Edit</p>
<form action="/countries/{{ $country->id }}/delete" method="post">
<input name="_method" type="hidden" value="DELETE">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
</div>
</div>
not working, I'm stuck at url:
http://192.168.1.7:8000/countries/6/delete
saying '404|not found'
You don't need to append /delete in the URL.
Try this:
<form action="/countries/{{ $country->id }}" method="post">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
As mentioned by #nakov, you can also remove the hidden input field. The blade directive #method('DELETE') is sufficient to do the job.
Hope it helps!

Laravel Eloquent to php

How to make delete function in laravel model, usually when in blade we use :
<form action="{{ action('ItemNameController#destroy', $ItemName->id) }}" method="post">
#csrf
#method("DELETE")
<input type="submit" class="btn btn-danger btn-sm btn-style" href="{{ $ItemName->id }}" value="Delete" onclick="return confirm('Are You Sure To Delete This Item? #{{ $ItemName->inc }} ')">
</form>
So how to write that code when we write it in controller, because in controller cannot to write eloquent or cannot to process (.blade.php)
Example in Controller:
foreach ($i as $value) {
echo "<form action='' method='post'>";
echo "<th><input type='submit' class='btn btn-danger btn-sm btn-style' href=' value='Delete' value='Delete' style='font-size: 10px'></th>";
echo "</form>";
}
You can achieve a this by:
<form action="{{ action('ItemNameController#destroy', $ItemName->id) }}" method="post">
{{csrf_field()}}
<input type="submit" class="btn btn-danger btn-sm btn-style" href="{{ $ItemName->id }}" value="Delete" onclick="return confirm('Are You Sure To Delete This Item? #{{ $ItemName->inc }} ')">
</form>
Where in your controller you would handle the delete method. If you were using an ajax request you could send a delete request to the server.
Your route something like be:
Route::post('/itemname/{ItemName}/delete', 'ItemNameController#destroy');
To stop it being confused with:
Route::post('itemname/{ItemName}', 'ItemNameController#update);
Hope this helps.

Laravel 5.2 - Form do not submit

Sorry, i'm very new to Laravel, i have this form:
<form action="/register" method="post">
{!! csrf_field() !!}
....
<div class="form-group">
<button type="button" class="btn btn-default">Register</button>
</div>
</form>
And the route is:
Route::get('/register', 'Auth\AuthController#getRegister');
Route::post('/register', 'Auth\AuthController#postRegister');
When i click on Register button nothing happen... the form do not submit, no errors etc.
Can you help me ?
Change this
<button type="button" class="btn btn-default">Register</button>
to
<button type="submit" class="btn btn-default">Register</button>
and always use dynamic url so that when you deploy your app to server,then you get no error(s)
action="{{ url('/register') }}
Change the type of the button:
<button type="submit"

Destroy - Laravel 5.2

Do not delete that line of mysql. What is the problem ? D
Controller
public function destroy($id) {
$seriale = Serial::find($id);
$seriale->delete();
return redirect()->route('admin.seriale.index');
}
View
<form action="{{ route('admin.seriale.destroy',$seriale->id) }}" method="DELETE" role="form">
<button type="submit" class="btn btn-danger">Sterge Serial</button>
</form>
Fix Your form with this notation.
This helper adds special hidden fields to implement DELETE request.
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.seriale.destroy', $seriale->id] ]) }}
<button type="submit" class="btn btn-danger">Sterge Serial</button>
{{ Form::close() }}
Read this question and answers: https://laracasts.com/index.php/discuss/channels/general-discussion/how-to-updatedelete-using-forms-and-restful-controllers?page=1
You can not send DELETE requests easily. Browsers only understand GET and POST. You have to use a magic field to tell Laravel that this is a DELETE request:
<form action="{{ route('admin.seriale.destroy',$seriale->id) }}" method="POST" role="form">
{{ csrf_field() }}
<!-- <input type="hidden" name="_method" value="DELETE"> -->
{{ method_field('delete') }} <!-- helper functions in laravel are awesome -->
<button type="submit" class="btn btn-danger">Sterge Serial</button>
</form>
I also added the csrf field to the form, you might need it.

Resources