laravel 如何使用 Eloquent 删除表中的所有行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15484404/
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-09 02:24:19  来源:igfitidea点击:

How to delete all the rows in a table using Eloquent?

laravellaravel-4eloquent

提问by Pete

My guess was to use the following syntax:

我的猜测是使用以下语法:

MyModel::all()->delete();

But that did not work. I'm sure it's super simple, but I've searched for documentation on the subject and can't find it!

但这没有用。我确定它非常简单,但我已经搜索了有关该主题的文档,但找不到!

回答by bilalq

The reason MyModel::all()->delete()doesn't work is because all()actually fires off the query and returns a collection of Eloquent objects.

原因MyModel::all()->delete()不起作用是因为all()实际上触发了查询并返回了 Eloquent 对象的集合。

You can make use of the truncate method, this works for Laravel 4 and 5:

您可以使用 truncate 方法,这适用于 Laravel 4 和 5:

MyModel::truncate();

That drops all rows from the table without logging individual row deletions.

这会删除表中的所有行,而不记录单个行的删除。

回答by Yauheni Prakopchyk

Laravel 5.2+solution.

Laravel 5.2+解决方案。

Model::getQuery()->delete();

Just grab underlying builder with table name and do whatever. Couldn't be any tidier than that.

只需使用表名获取底层构建器并执行任何操作。没有比这更整洁的了。

Laravel 5.6solution

Laravel 5.6解决方案

\App\Model::query()->delete();

回答by Fortex

You can use Model::truncate()if you disable foreign_key_checks(I assume you use MySQL).

您可以使用Model::truncate(),如果你禁用foreign_key_checks(我假设你使用MySQL)。

DB::statement("SET foreign_key_checks=0");
Model::truncate();
DB::statement("SET foreign_key_checks=1");

回答by giannis christofakis

I've seen both methods been used in seed files.

我已经看到这两种方法都用于种子文件。

// Uncomment the below to wipe the table clean before populating

DB::table('table_name')->truncate();

//or

DB::table('table_name')->delete();

Even though you can not use the first one if you want to set foreign keys.

如果您想设置外键,即使您不能使用第一个。

Cannot truncate a table referenced in a foreign key constraint

无法截断外键约束中引用的表

So it might be a good idea to use the second one.

所以使用第二个可能是个好主意。

回答by Rejaul

There is an indirect way:

有一种间接的方式:

myModel:where('anyColumnName', 'like', '%%')->delete();

Example:

例子:

User:where('id', 'like' '%%')->delete();

Laravel query builder information: https://laravel.com/docs/5.4/queries

Laravel 查询构建器信息:https://laravel.com/docs/5.4/queries

回答by lookitsatravis

I wanted to add another option for those getting to this thread via Google. I needed to accomplish this, but wanted to retain my auto-increment value which truncate()resets. I also didn't want to use DB::anything because I wanted to operate directly off of the model object. So, I went with this:

我想为那些通过 Google 访问此线程的人添加另一个选项。我需要完成这个,但想保留我truncate()重置的自动增量值。我也不想使用DB::任何东西,因为我想直接操作模型对象。所以,我去了这个:

Model::whereNotNull('id')->delete();

Obviously the column will have to actually exists, but in a standard, out-of-the-box Eloquent model, the idcolumn exists and is never null. I don't know if this is the best choice, but it works for my purposes.

显然,该列必须实际存在,但在标准的、开箱即用的 Eloquent 模型中,该id列存在并且永远不会为空。我不知道这是否是最好的选择,但它适用于我的目的。

回答by Dave James Miller

I wasn't able to use Model::truncate()as it would error:

我无法使用,Model::truncate()因为它会出错:

SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint

SQLSTATE[42000]:语法错误或访问冲突:1701 无法截断外键约束中引用的表

And unfortunately Model::delete()doesn't work (at least in Laravel 5.0):

不幸的Model::delete()是不起作用(至少在 Laravel 5.0 中):

Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically, assuming $this from incompatible context

非静态方法 Illuminate\Database\Eloquent\Model::delete() 不应静态调用,假设 $this 来自不兼容的上下文

But this does work:

但这确实有效:

(new Model)->newQuery()->delete()

That will soft-delete all rows, if you have soft-delete set up. To fully delete all rows including soft-deleted ones you can change to this:

如果您设置了软删除,这将软删除所有行。要完全删除所有行,包括软删除的行,您可以更改为:

(new Model)->newQueryWithoutScopes()->forceDelete()

回答by Pete

The best way for accomplishing this operation in Laravel 3seems to be the use of the Fluentinterface to truncate the table as shown below

完成这个操作的最好方法Laravel 3似乎是使用Fluent界面截断表格,如下图

DB::query("TRUNCATE TABLE mytable");

回答by jfeid

You can try this one-liner which preserves soft-deletes also:

您可以尝试使用这种单行代码,它也可以保留软删除:

Model::whereRaw('1=1')->delete();

回答by ali filali

simple solution:

简单的解决办法:

 Mymodel::query()->delete();