[英]Laravel Eloquent delete() not working
Documentation says:文档说:
$user = User::find($user_id);
$user->delete();
This doesnt work, however ProductColor::find($color_id) working.这不起作用,但是 ProductColor::find($color_id) 工作。 $color->delete() Doest return anything, DELETE FROM query doesn't even execute (as seen in debug bar).
$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';
}
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 抱怨使用非静态方法 'find'
Your code is fine - the docs show exactly what you are doing.您的代码很好 -文档准确显示了您在做什么。
If there is no error, and the color is not deleted as expected, then $color_id
is not being passed as expected.如果没有错误,并且颜色没有按预期删除,则
$color_id
没有按预期传递。 Try using findOrFail
, or add some other check that you found the expected model.尝试使用
findOrFail
,或添加一些您找到预期模型的其他检查。
如果您添加了软删除选项,请检查您的模型类,在这种情况下,您的记录不会从数据库中删除。
DB::table('pykcodes')
->where('user_id', $user_id)
->delete();
This works directly without using the model.这无需使用模型即可直接工作。 Just make sure you call
use DB;
只要确保你调用
use DB;
from the top.从一开始。
My problem was, the name of the property inside the uri
on the routes file was incorrect:我的问题是,路由文件中
uri
内的属性名称不正确:
Route::delete('/foo/{fo3o}', [DollyController::class, 'delete']);
-----^
I don't know why the controller method was working:我不知道为什么控制器方法有效:
public function delete(Foo $foo)
{
$foo->delete();
return ['message' => 'success'];
}
After correcting it, the problem was solve and the delete method started working again:更正后,问题解决了,删除方法又开始工作了:
Route::delete('/foo/{foo}', [DollyController::class, 'delete']);
------^
You have to add SoftDeletes in User Model:您必须在用户模型中添加 SoftDeletes:
... ...
use Illuminate\Database\Eloquent\SoftDeletes;
使用 Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {类用户扩展 Authenticatable {
use SoftDeletes;
使用软删除;
protected $dates = ['deleted_at'];
受保护的 $dates = ['deleted_at'];
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.