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

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

    Solution who works with Lumen 5.5 with foreign keys constraints :

    $categories = MusicCategory::all();
    foreach($categories as $category)
    {
    $category->delete();
    
    }
    return response()->json(['error' => false]);
    
    0 讨论(0)
  • 2021-01-30 10:41

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

    DB::query("TRUNCATE TABLE mytable");
    
    0 讨论(0)
提交回复
热议问题