delete a table form a database using laravel command

i need to delete a database table using laravel artisan command . not like this command php artisan migrate:rollback --step=5

i need to create like this route or controller code .

Route::get('/clear/database', function () {

    Artisan::call('cache:clear');
    return redirect('/');

});

. i also try public function dd()

{
    Schema::drop('table_name');
}

but it not working . gives me error like this SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table table_name)

no foreign key for the table . what should i do ?

thanks in advance!

1 answer

  • answered 2022-05-07 06:05 Hirbod

    it's because one of your tables has a fk that is pointing to the table you are trying to delete

    use Illuminate\Support\Facades\DB;
    
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    
    Schema::dropIfExists('table');
    
    // or
    
    Artisan::call('command');
    
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum