Destroy - Laravel 5.2 - laravel

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.

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 delete method not working

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?

getting error Missing required parameters for [Route: shortlist.update] [URI: recruiter/shortlistt/{id}/{jid}]

this is my view page route
<form method='post' action={{ route('shortlist.update', ['id' => $dd->jobseekers_unique_id, 'jid' => $dd->job_unique_id ]) }}>
{{ csrf_field() }} {{ method_field('PATCH') }}
<button type='submit' class="col-md-5 col-sm-5 btn btn-success">Shortlist</button>
</form>
and this my main route
Route::patch('shortlistt/{id}/{jid}','RecruiterController#shortlisted')->name('shortlist.update');
But it's giving me the error and I don't know how to make it work.
You have syntax error in your code, you missed the quotes for action. Here is your updated code.
<form method='post' action="{{ route('shortlist.update', ['id' =>
$dd->jobseekers_unique_id, 'jid' => $dd->job_unique_id ]) }}">
{{ csrf_field() }} {{ method_field('PATCH') }}
<button type='submit' class="col-md-5 col-sm-5 btn btn-success">Shortlist</button>
</form>
Hope it helps.

Method Not Allowed HTTP Exception in Laravel 5.4

I have a form with two text fields and two buttons (edit and delete), when I press edit button, it works fine but when I press delete button, it gives the 'MethodNotAllowedHttp' exception. My code is as follows:
<form action="/laboratory/doctors/update" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
My Routes are as follows:
Route::post('/doctors/update', 'DoctorsController#update');
Route::delete('/doctors/{doctor}/delete', 'DoctorsController#destroy');
Any help is appreciated.
You have created route of http verbs as delete type and trying to get of type get. So you can change it as get instead delete
Route::get('/laboratory/doctors/{doctor}/delete', 'DoctorsController#destroy');
Also there is other way to make http verb as DELETE but using
Another form tag outside of current form tag.
Or use ajax to make it delete type
Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
DELETE
{{ Form::open(['url' => '/laboratory/doctors/{{ $doctor->id }}/delete', 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn btn-danger']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding {doctor} of the delete post so you dont have multiple html forms in your code.
#if(isset($doctor))
<form action="/laboratory/doctors/{{$doctor->id}}/delete" method="POST">
#else
<form action="/laboratory/doctors/update" method="POST">
#endif
{{ csrf_field() }}
#if(isset($doctor))
{{ method_field('DELETE') }}
#endif
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
The problem is in your action.. when you click delete button, the button run the destroy function, but the form run the update function. So, you have to separate the form action, or you can delete it by using ajax

Laravel 5.0 destroy method

I can figure out why i am not accessing me destroy method.
Blade:
#foreach($advertisements as $advertisement)
<form class="form-horizontal" method="delete" action="advertisements/{{ $advertisement->id }}" accept-charset="UTF-8">
<div class="form-group">
<h2> {{ $advertisement->title }}</h2>
{{$advertisement->city}} {{ $advertisement->type }}
<input class="btn-danger" type="submit" value="Delete add">
</div>
</form>
Controller:
public function destroy($advertisements)
{
Advertisement::find($advertisements)->delete();
return redirect('advertisements');
}
route:list
| DELETE | advertisements/{advertisements} | advertisements.destroy | App\Http\Controllers\AdvertisementsController#destroy
From official laravel docs,
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:
You should update your form tag and add a hidden input field _method:
<form class="form-horizontal" method="POST" action="advertisements/{{ $advertisement->id }}" accept-charset="UTF-8">
<input type="hidden" name="_method" value="DELETE">

Resources