2017-08-13 61 views
1

文件说:Laravel雄辩删除()不工作

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

$user->delete(); 

这不工作,但是ProductColor ::发现($ COLOR_ID)工作。 $ color-> delete()Doest返回任何东西,DELETE FROM查询甚至不会执行(如调试栏所示)。

但我可以删除记录:

ProductColor::destroy($color_id); 

一定有什么东西我刚才忽略了,我是新来的Laravel。

我使用的储存也和它的工作如预期

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'); 
     } 

    } 

综上所述

这个作品

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'); 
     } 
    } 

这难道不是

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

    $color->delete(); 
} 

我的模型

<?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'; 
} 
+0

真的我不不知道为什么这不起作用。但是,你为什么不试试查询? '$ user = User :: where('user_id',$ user_id) - > delete();' –

+0

我的IDE显示'where'不是静态方法,所以我只使用User :: destroy($ user_id); – RomkaLTU

+0

当您尝试使用delete()时,错误是什么? – lesssugar

回答

0

对不起,我已经弄清楚问题出在哪里。我的错误发布问题。

我试图这样做:

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

应该是:

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

我的问题是taht我吓得IDE抱怨使用非静态方法 '发现'

0

你的代码很好 - the docs show exactly你在做什么。

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