Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop table in Laravel?

Tags:

laravel-5

The problem is that I have this error:

[PDOException]

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'songs' already exists

This is my migration file:

<?php  use Illuminate\Database\Schema\Blueprint;  use Illuminate\Database\Migrations\Migration;  class CreateSongsTable extends Migration  {     public function up()      {         Schema::create('songs', function (Blueprint $table)          {             $table->increments('id');             $table->integer('user_id');             $table->string('title');             $table->string('slug')->unique();             $table->timestamps();             $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');         });     } } 

I think the solution will be just to delete the table then run migration again, so how can I drop a table in Laravel 5 using the command line? I am using MySQL.

like image 918
koooko Avatar asked Jul 05 '15 10:07

koooko


People also ask

How do I drop a table in migration?

First generate an empty migration with any name you'd like. It's important to do it this way since it creates the appropriate date. The only thing I added was drop_table :products and raise ActiveRecord::IrreversibleMigration . Then run rake db:migrate and it'll drop the table for you.


2 Answers

To drop a table, you may use the Schema::drop method:

Schema::drop('users');  // Better Schema::dropIfExists('users'); 
like image 83
Akbor Avatar answered Sep 30 '22 09:09

Akbor


To drop a table in laravel, Create a first migration

Step to drop a table

$ php artisan make:migration drop_user_table 

Add this to your migrate file inside up function Schema::drop('tableName');

public function up()  {     Schema::dropIfExists(table('songs'));     $table->increments('id');     ...  } 

then run

$ php artisan migrate 
like image 44
Khadreal Avatar answered Sep 30 '22 09:09

Khadreal