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.
To drop a table, you may use the Schema::drop method:
Schema::drop('users');
// Better
Schema::dropIfExists('users');
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');
$ php artisan migrate
Nice way to drop an existing table, you may use the drop or dropIfExists methods:
Schema::drop('users');
Schema::dropIfExists('users');
You can also rollback if you wanna drop your last migration table
php artisan migration:rollback
The migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
The migrate:fresh command will drop all tables from the database and then execute the migrate command:
php artisan migrate:fresh
php artisan migrate:fresh --seed
You need a down method on your migration so that when you run php artisan migrate:rollback
it can drop your database.
e.g.
<?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');
});
}
public function down()
{
Schema::drop('songs');
}
}
The simple way to Delete the table and run the Migration again. Just run the artisan command.
php artisan migrate:fresh
Note: It will drop all tables and re-run migration.
or if you have the seed of tables then run this command
php artisan migrate:fresh --seed
Reference: Laravel Documentation
A nice easy way that I found is just using phpmyadmin and just drop the table manually. Of course if the migration is still there when you run the migration again the table will be created again.