为什么方法欺骗不能使用Laravel?

时间:2017-05-22 10:36:30

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

我有一个与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方法不是从数据库中删除记录而且没有任何错误。它在不删除记录的情况下发出会话消息。

2 个答案:

答案 0 :(得分:0)

我看到你的另一个主题有关于分离导致问题的相同问题。

将此添加到您的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');
}

答案 1 :(得分:0)

你必须使用&#34; - &gt; destroy()&#34;在控制器中。 请查看此问题,以便在destroy() and delete()

之间进行说明
相关问题