1

hi if i have data in laravel like this ..

$data = MyModel::get();

or maybe my data is an relation one to many like this ..

$data->MyModel['get_one_to_many_relation'];

how can i delete all data like this ..

$data->delete;

in normal way i delete like this ..

foreach($data as $d)
{
   $d->delete();
   // this will delete them but i have to write the foreach
}

so is it possable to delete them without foreach like this ..

$data->delete();
softya
  • 229
  • 7
  • 22
  • 1
    Does this answer your question? [How to delete all the rows in a table using Eloquent?](https://stackoverflow.com/questions/15484404/how-to-delete-all-the-rows-in-a-table-using-eloquent) – Oleg Nurutdinov Apr 25 '20 at 18:44

1 Answers1

1

You can delete all your relations using:

$data->get_one_to_many_relation()->delete();

And then delete your $data

MyModel::query()->delete();
Christophe Hubert
  • 2,833
  • 1
  • 12
  • 25