Laravel Eloquent delete() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45659006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:29:58  来源:igfitidea点击:

Laravel Eloquent delete() not working

phplaravellaravel-5eloquentlaravel-eloquent

提问by RomkaLTU

Documentation says:

文档说:

$user = User::find($user_id);

$user->delete();

This doesnt work, however ProductColor::find($color_id) working. $color->delete() Doest return anything, DELETE FROM query doesn't even execute (as seen in debug bar).

这不起作用,但是 ProductColor::find($color_id) 工作。$color->delete() 不返回任何内容,DELETE FROM 查询甚至不执行(如调试栏中所示)。

But I can delete record with:

但我可以删除记录:

ProductColor::destroy($color_id);

Must be something I overlooked earlier, I'm new to Laravel.

一定是我之前忽略的东西,我是 Laravel 的新手。

I'm using store also, and it working as expected

我也在使用商店,它按预期工作

public function store(Request $request)
    {
        $color = new ProductColor();
        $color->name = $request->color_name;
        $color->order = $request->color_order;
        $saved = $color->save();

        if ($saved) {
            return back()->with('message:success', 'Ok');
        } else {
            return back()->with('message:error', 'Error');
        }

    }

To sum up

总结

This WORKS

这有效

public function destroy($color_id)
    {
        $deleted = ProductColor::destroy($color_id);

        if ($deleted) {
            return back()->with('message:success', 'Deleted');
        } else {
            return back()->with('message:error', 'Error');
        }
    }

This NOT

这不是

public function destroy($color_id)
{
  $color = ProductColor::find($color_id);

  $color->delete();
}

My Model

我的模特

<?php

namespace Modules\Shop\Entities;

use Illuminate\Database\Eloquent\Model;
use Modules\Languages\Entities\Language;

class ProductColor extends Model
{
    protected $fillable = [];
    protected $table = 'product_colors';
}

采纳答案by RomkaLTU

Sorry, I've figured out the problem. My mistake to post this question.

对不起,我已经找到了问题所在。发布这个问题是我的错误。

What I tried to do:

我试图做的事情:

$color = new ProductColor();
$color->find($color_id);
$color->delete();

Should be:

应该:

$color = ProductColor::find( $color_id );
$color->delete();

My problem was that I was scared about the IDE complaining about using non-static method 'find'

我的问题是我害怕 IDE 抱怨使用非静态方法“查找”

回答by Don't Panic

Your code is fine - the docs show exactlywhat you are doing.

您的代码很好 -文档准确地显示了您在做什么。

If there is no error, and the color is not deleted as expected, then $color_idis not being passed as expected. Try using findOrFail, or add some other check that you found the expected model.

如果没有错误,并且颜色没有按预期删除,则$color_id没有按预期传递。尝试使用findOrFail,或添加一些其他检查以确保您找到了预期的模型。

回答by Julius Garcia

You have to add SoftDeletes in User Model:

您必须在用户模型中添加 SoftDeletes:

...

...

use Illuminate\Database\Eloquent\SoftDeletes;

使用 Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable {

类用户扩展可验证{

use SoftDeletes;

protected $dates = ['deleted_at'];

使用软删除;

受保护的 $dates = ['deleted_at'];

...

...