I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Below are route listing:
HTML forms don't actually support any methods other than GET and POST. To get around this Laravel spoofs the method and then picks this up in the request.
From the 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
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method with {{ method_field('DELETE') }} using Blade.
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Related
I am trying to use Resource Controller and having a problem with destroying method, can't find a solution.
I get this error
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
web.php
Route::resource('honor', 'HonorController');
HonorController.php
public function destroy(Honor $honor)
{
dd($honor);
$honor->delete();
return redirect()->back();
}
blade
<form action="{{ route('honor.destroy', $honor->id) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>
Try changing the key (uses honor, not id) to match the name of the parameter in the route.
For example:
<form action="{{ route('honor.destroy', ['honor' => $honor->id]) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>
I want delete a comment, but when i click, this redirect me to laravel error page and display "" that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
#foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
#endforeach
If you add {{ method_field('DELETE') }} on the html form, you must change the route method to delete:
Route::delete('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
But if you want to use POST method on the route, you must remove {{ method_field('DELETE') }} from your form.
I am following a tutorial about Laravel.
However, I want to convert the blade template Form::open() to html/php form, to make it easier to read & understand.
This is the Blade template:
{{ Form::open(['action'=> ['StudentController#destroy', $student->id], 'method'=>'POST']) }}
{{ method_field('DELETE') }}
{{ Form::submit('Delete',['class'=>'btn btn-danger']) }}
{{ Form::close() }}
I need to convert the blade code to html/php
I tried it multiple times, something like this. but failed.
<form action="url('StudentController#destroy', $student->id)" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Anyone know the correct html/php form?
[edit] Route:list
try this way
use {{}} and use route
<form action="{{route('StudentController#destroy', ['id'=>$student->id])}}" method="POST">
<?php method_field('Delete'); ?>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
To call a controller action you need to use url()->action(...) (or action()) for short.
<form action="{{url()->action('StudentController#destroy', ['id'=>$student->id])}}" method="POST">
#csrf
{{ method_field('DELETE'); }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
This is also described in the manual
you should use this code
<form action="{{ url('StudentController#destroy', $student->id) }}" method="POST">
<input type='_method' value='DELETE' />
<button class="btn btn-danger" type="submit">Delete</button>
</form>
In your 'action' on the form, you need to enclose any helper functions within brackets so that Blade knows what to do with this, otherwise, it's just text.
Also note, I removed 'method_field' and replaced it with the hidden field, as this is essentially what method_field helper creates.
<form action="{{route('StudentController#destroy', ['id' => $student->id])}}" method="POST">
<input type='hidden' value='DELETE'>
<button class="btn btn-danger" type="submit">Delete</button>
</form>
If using the route helper isn't working, you could use a more simple approach for the 'action' param of the form tag:
<form action="/student/destroy/{{$student->id}}" method="POST">
How can I create multiple requests for the same route like below.
Route.php
Route::get('/home', 'HomeController#index');//->middleware('auth');
Route::get('/home/{$user}','HomeController#showStudent');
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
the form was working fine until I have added the delete request. In my blade template I have code something like this.
home.blade.php
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
{{ csrf_field() }}
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
I believe because of the same routes it's showing NotFoundHTTPException.
On one route /home I am trying to Add, Show, Edit and Delete a record with different buttons.
Thanks in Advance.
You could add a form and use Laravel's Form Method Spoofing
<input type="hidden" name="_method" value="DELETE">
See more here...http://laravel.com/docs/master/routing#form-method-spoofing
Try as below....
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
1) Change you route from:
Route::delete('/home/{$studentId}','HomeController#deleteStudent');
To:
Route::get('/delete/{$Id}','HomeController#deleteStudent')->name('delete');
2) change you form tag from:
<form class="" role="form" method="DELETE" action="/home/{{$student->id}}">
To:
<form class="" role="form" method="get" action="route('delete', ['id' => $student->id])">
HTML forms doesn't support methods other than get and post. If you need to simulate it, include a hidden input to simulate delete:
<input name="_method" type="hidden" value="DELETE">
Then in your code, update it to:
<form class="" role="form" method="POST" action="/home/{{$student->id}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td>
</form>
Reference:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
http://laraveldaily.com/theres-no-putpatchdelete-method-or-how-to-build-a-laravel-form-manually/
I'm having issues with getting a laravel app to update or delete a resource.
Here is my view.
#extends('admin.master')
#section('content')
<h1>Create an Article</h1>
<form action="/articles/{{ $article->id }}">
<input type="hidden" name="_method" value="PUT">
{!! csrf_field() !!}
#include('admin.partials.forms.article')
<div class="row">
<button type="submit" class="btn btn-success btn-lg">Update Article</button>
</div>
</form>
#endsection
Here is my controller
public function update($id, Request $request)
{
return "Update Article Code Here!";
}
All I get when I submit the form is a blank page with the url
app.dev/articles/1?_method=PUT&_token=LL6Z5zHNUG1dLjjH2TDpXXCWbGnfiCKTY4cuoVbm&title=Our+Upcoming+Event+Now+Updated&description=a+brief+event+description&body=Updated+Body&category=Events
The issues is that while you have to have the hidden method to allow laravel to see what you're doing, you also have to have the method="POST".
<form action="/articles/{{ $article->id }}" method="POST">
<input type="hidden" name="_method" value="PUT">