如何使用 Laravel 发出删除请求 [英] How to make a delete request with Laravel

查看:17
本文介绍了如何使用 Laravel 发出删除请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用资源控制器.

I am not using resource controller.

路线:

Route::delete('/deleteTag/{tag}','Controller2@deleteTag');

控制器功能:

public function deleteTag(Tag $tag){
  $Tag = Tag::where('id', $tag->id)->get()->first();
  $Tag->delete();
  return redirect()->action('Controller2@main');
}

电话:

<form method="delete" action="http://***/public/deleteTag/{{$tag->id}}"> 
    {!! Form::token() !!} 
    <button type="submit">delete</button>
</form>

程序返回 MethodNotAllowedHttpException.

The program returns a MethodNotAllowedHttpException.

谢谢.

推荐答案

你可以试试这个(注意隐藏的 _method 输入):

You may try this (Notice the hidden _method input):

<form method="post" action="http://***/public/deleteTag/{{$tag->id}}"> 
    {!! Form::token() !!}
    <input type="hidden" name="_method" value="DELETE">
    <button type="submit">delete</button>
</form>

检查表单方法欺骗.

在最新版本的 Laravel 中,可以对 csrfmethod 使用刀片指令,例如:

In the latest versions of Laravel, it's possible to use blade directives for csrf and method in the form, for example:

<form method="post" action="..."> 
    @csrf
    @method('DELETE')
    <button type="submit">delete</button>
</form>

这篇关于如何使用 Laravel 发出删除请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆