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

前端 未结 14 2076
鱼传尺愫
鱼传尺愫 2021-01-30 09:35

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

14条回答
  •  借酒劲吻你
    2021-01-30 10:25

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

    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.

提交回复
热议问题