【问题标题】:Why Is Method Spoofing not Working Laravel?为什么方法欺骗在 Laravel 中不起作用?
【发布时间】:2017-10-21 23:39:21
【问题描述】:

我有一个与 destroy 方法关联的表单来删除项目/记录。

<form action="{{ route('climb-excluded.destroy',$exclude->id) }}" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <button type="submit" class="btn btn-danger btn-sm">
        <i class="fa fa-trash-o" aria-hidden="true"></i> Delete
    </button>
</form>

destroy() 方法

<?php

public function destroy(Cexcluded $cexcluded)
{
    $cexcluded->tours()->detach();
    $cexcluded ->delete();
    Session::flash('success','Item sucessfully deleted !');
    return redirect()->route('climb.ie');
}

路线

 Route::resource('climb-excluded','CexcludedsController', ['only'=>['store','destroy']]);
 Route::get('climbing/included-excluded','HomeController@getclimbIE')->name('climb.ie');  

我遇到的问题是 destroy 方法没有从数据库中删除记录并且没有给出任何错误。它在不删除记录的情况下发出会话消息。

【问题讨论】:

    标签: php mysql laravel laravel-5 laravel-5.4


    【解决方案1】:

    我看到您的另一个线程也遇到了关于分离导致问题的相同问题。

    将此添加到您的 Cexcluded 模型中

    public static function boot()
    {
        parent::boot();
    
        static::deleting(function($model) {
            $model->tours()->detach();
        });
    }
    

    从您的控制器方法中删除以下行

    $cexcluded->tours()->detach();
    

    所以试试这个

    public function destroy(Cexcluded $cexcluded)
    {
        $cexcluded ->delete();
        Session::flash('success','Item sucessfully deleted !');
        return redirect()->route('climb.ie');
    }
    

    【讨论】:

    • 感谢您的建议。但它也没有奏效。所以我将我的销毁方法更改为旧的常规方式。public function destroy($id) { $cexcluded = Cexcluded::find($id); $cexcluded-&gt;tours()-&gt;detach(); $cexcluded -&gt;delete(); Session::flash('success','Item sucessfully deleted !'); return redirect()-&gt;route('climb.ie'); }
    • 分离关系给你带来的问题真的很奇怪。无论如何,你得到它的工作很好。
    【解决方案2】:

    您必须在控制器中使用“->destroy()”。 在destroy() and delete()

    之间检查这个问题以获得澄清

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 2018-12-16
      • 2012-01-29
      • 2010-10-11
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多