如何使用 Eloquent 删除表中的所有行? [英] How to delete all the rows in a table using Eloquent?

查看:26
本文介绍了如何使用 Eloquent 删除表中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

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

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

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

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

MyModel::truncate();

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

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

这篇关于如何使用 Eloquent 删除表中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆