问题描述
我是Laravel的业余爱好者.我使用laravel 5.4.所以我想在没有表单绑定的情况下删除进程,但是我收到了这样的错误消息.请告诉我如何解决这个问题.
I'm an amateur in laravel. I use laravel 5.4. so I want to make process delete without form binding but I have an error message like this. Please tell me how to solving this.
路线:
Route::delete('test/{id}','TestController@destroy');
我的表格:
<td><button type="button" class="btn"><a href="{{URL::to('coba/test/'.$post->id.'/edit') }}" >Edit</a></button><button type="button" class="btn"><a href="{{ action('TestController@destroy', $post['id']) }}" method="post" >Hapus</a></button>{{ csrf_field() }}{{ method_field('DELETE') }}
</td>
我的控制器:
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect()->to('coba/test');`
}
推荐答案
锚点html元素上的Href将导致GET调用,但您的路线需要Delete调用.您可以通过某些方法来确保您将导致删除呼叫.
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.
删除
{{ Form::open(['url' => 'test/'.$post->id, 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
修改
{{ Form::open(['url' => 'coba/test/'.$post->id.'/edit', 'method' => 'POST']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn']) }}
{{ Form::close() }}
出于最佳实践,我建议只使用一次{{ Form::open(...) }} {{ Form::close() }}
并重构您的控制器代码,以便它可以从按钮中读取值并将其转换为帖子的相应ID,因此您的代码中没有多个html形式.
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 id of the post so you dont have multiple html forms in your code.
这篇关于laravel 5.4 MethodNotAllowedHttpException在RouteCollection.php中(第251行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!