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

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

    In a similar vein to Travis vignon's answer, I required data from the eloquent model, and if conditions were correct, I needed to either delete or update the model. I wound up getting the minimum and maximum I'd field returned by my query (in case another field was added to the table that would meet my selection criteria) along with the original selection criteria to update the fields via one raw SQL query (as opposed to one eloquent query per object in the collection).

    I know the use of raw SQL violates laravels beautiful code philosophy, but itd be hard to stomach possibly hundreds of queries in place of one.

提交回复
热议问题