adamnet's avatar

Run Mysql script in Laravel

When I want to truncate a table which has foreign key cobstraints I will use phpmyadmin and will run a script as follows set foreign_key_checks=0; truncate table mytable; set foreign_key_checks=1; Is it possible to run this through some method in Laravel?

0 likes
4 replies
lpheller's avatar

Schema::disableForeignKeyConstraints();
DB::table('mytable')->truncate();
Schema::enableForeignKeyConstraints();

// or "raw"

DB::statement('SET FOREIGN_KEY_CHECKS=0');
DB::statement('TRUNCATE TABLE mytable');
DB::statement('SET FOREIGN_KEY_CHECKS=1');
1 like
martinbean's avatar

@adamnet Why would you not just run php artisan migrate:fresh? Why are you trying to perform such destructive actions from inside the application itself?

NekaDava's avatar

Most likely his goal is to remove a single table, but not to drop the entire database.

Please or to participate in this conversation.