文件说:
$user = User::find($user_id);
$user->delete();
这是不行的,但是产品颜色::发现($颜色标识)工作。 $ color->删除()不返回任何东西,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';
}
对不起,我必须找出其中的问题。我的错误张贴问题。
我试图这样做:
$color = new ProductColor();
$color->find($color_id);
$color->delete();
应该:
$color = ProductColor::find( $color_id );
$color->delete();
我的问题是,我吓坏了IDE抱怨使用非静态方法“查找”
您的代码是好的 - the docs show exactly你在做什么。
如果没有错误,并如预期的颜色不会被删除,那么$color_id
未如预期顺利通过。尝试使用findOrFail
,或添加一些其他的检查,你发现了预期模型。
你必须添加在用户模型SoftDeletes:
...
使用照亮\数据库\雄辩\ SoftDeletes;
类用户可验证延伸{
使用SoftDeletes;
保护$日期= [ 'deleted_at'];
...