TRUNCATE ... CASCADE in Laravel 5

It should be simple, but I cannot find it in the documentation or elsewhere. How to TRUNCATE...CASCADE table with foreign keys in Laravel 5.1 using Eloquent ORM? Using only User::truncate(); does not go through the CASCADE argument.

+6
source share
2 answers

It seems the only way to do this is to run the statement directly:

 DB::statement('TRUNCATE users CASCADE'); 
+7
source

If you need to truncate more than one table, you can remove the CASCADE keyword and provide a comma-separated list of table names.

  $tables = [ 'roles', 'role_users', 'users', 'revisions', ]; if (count($tables) > 1) { DB::statement('TRUNCATE TABLE ' . implode(',', $tables) . ';'); } else { DB::statement('TRUNCATE TABLE ' . $tables[0] . ' CASCADE;'); } 
0
source

All Articles