Eloquent删除不适用于laravel 5.6

时间:2018-04-05 04:20:43

标签: php laravel eloquent laravel-5.6

这是我的代码:

路线

Route::resource('ItemName', 'ItemNameController');

控制器

public function destroy(ItemName $itemName)
{
    $itemName->delete();
    return redirect('ItemName')->with('success', 'Item Has Been Delete');
}

查看

<form action="{{ route('ItemName', $ItemName->id) }}" method="post">
    @csrf
    @method("DELETE")
    <input type="submit" class="btn btn-danger btn-sm" href="{{ $ItemName- 
    >id }}" value="Delete" onclick="return confirm('Are You Sure To Delete 
    This Item? #{{ $ItemName->inc }} ')">
</form>

问题是代码无法删除表中的项目,有什么帮助吗?感谢

3 个答案:

答案 0 :(得分:3)

在你的控制器中试试这个

public function destroy($id)
{
    ItemName::destroy($id); 
    return redirect('ItemName')->with('success', 'Item Has Been Delete');
}

或者你可以试试

public function destroy($id)
{
    $itemName = ItemName::find($id);
    $itemName ->delete(); 
    return redirect('ItemName')->with('success', 'Item Has Been Delete');
}

答案 1 :(得分:0)

像这样更改您的视图代码

<form action="{{ route('ItemName', $ItemName->id) }}" method="post" onSubmit="return confirm('Are You Sure To Delete 
    This Item? #{{ $ItemName->inc }} ')">
    @csrf
    @method("DELETE")
    <button class="btn btn-danger btn-sm">Delete</button>
</form>

或者您也可以设置此action="url('/ItemName/'. $ItemName->id)"

之类的操作

答案 2 :(得分:0)

试试这个 所以我的刀片是

   <a href="{{url('path.'/'.$row->id.'/soft-delete')}}" class="btn btn-sm btn-danger"> <i class="fa fa-trash-o"></i> </a>

然后控制器

public function soft_delete($id)
{
    if($this->data->find($id)->delete()) {
        return redirect()->back()->with('success', 'Your data has been moved to trash');
    }else {
        return redirect()->back()->with('error', 'Your data has not been moved to trash.');
    }
}

我的路线是

Route::get('path/{id}/soft-delete', 'EventController@soft_delete');

如果你想要softdelete使用softdeletes,否则不需要使用