How to throw a table in Laravel?

The problem is that I have this error:

[PDOException]

SQLSTATE [42S01]: base table or view already exists: 1050 "Songs" already exist

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 only be to delete the table and then start the migration again, so how can I drop the table in Laravel 5 using the command line? I am using MySQL.

+16
source share
6 answers

A good easy way I found is to just use phpmyadmin and just drop the table manually. Of course, if the migration still exists, when you start the migration again, the table will be created again.

-21
source

To delete a table, you can use the Schema :: drop method:

 Schema::drop('users'); // Better Schema::dropIfExists('users'); 
+36
source

To delete a table in laravel, create the first migration

Step to delete a table

 $ php artisan make:migration drop_user_table 

Add this to your migration file inside the Schema::drop('tableName'); function Schema::drop('tableName');

 $ php artisan migrate 
+17
source

A good way to delete an existing table is to use the drop or dropIfExists methods:

 Schema::drop('users'); Schema::dropIfExists('users'); 

You can also roll back if you want to delete your last migration table

 php artisan migration:rollback 

The migrate: reset command will roll back all the migrations of your application:

 php artisan migrate:reset 

The migrate: fresh command will remove all tables from the database, and then execute the migrate command:

 php artisan migrate:fresh php artisan migrate:fresh --seed 
+10
source

You need a down method when migrating so that when you run php artisan migrate:rollback it can delete your database.

eg.

 <?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'); } } 
+7
source

An easy way to delete a table and start the migration again. Just run the artisan team.

 php artisan migrate:fresh 

Note: It will drop all tables and re-run migration.

or, if you have table seed, run this command

 php artisan migrate:fresh --seed 

Link: Laravel Documentation

0
source

All Articles