Delete all rows in the table using Eloquent
For deleting all rows in the table using eloquent the method is:
MyModel::truncate();
This method can be used for Laravel 4 and 5 versions.
truncate() resets the AUTO_INCREMENT counter. You cannot do truncate if any foreign key constraints are present and truncate will not trigger delete events.
If you want to truncate a child table and parent table with foreign key reference, you can use the getQuery->delete method.
Model::getQuery()->delete();
For Laravel 5.6 use :
\App\Model::query()->delete();
If you disable the foreign key constraints Model::trunctate() can be used.
DB::statement("SET foreign_key_checks=0");Model::truncate();
DB::statement("SET foreign_key_checks=1");
Comments
0 comments
Please Sign in or Create an account to Post Comments