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

前端 未结 14 2077
鱼传尺愫
鱼传尺愫 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:36

    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.

提交回复
热议问题