Is there a way I could truncate all the tables in a db using eloquent or fluent in laravel 4? I do not want to specify table names, I just want to truncate all the tables. In other words empty all the tables.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
In laravel 5, migrate:fresh will drop all the tables in the database (even if tables aren't related to migrate)
Use this:
Remember you import
PD: Tables_in_YOUR_DATABASE_NAME
Based on previous answers, I filter table names directly into the SQL query. I'm agree it's a small optimization but that avoids unnecessary loop.
So Make Sure that is Installed
composer require doctrine/dbal
1. Get all the table names
2. Loop through the array of table names and truncate with Schema Builder
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint
You Can disable foriegn Key Checks
and make sure to ReEnable it
This is how i truncate all tables inside a database (including table exceptions), it works for me.
Here is my answer based on @Hao Luo. Moreover, it has these pros:
Here is the code:
Hope you like it! :)