I am working on laravel 9. Actually i want to create 2 tables in my database. I saved and next run 'php artisan migrate' in terminal and it pops up error:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'orders' already exists
(SQL: create table `orders` (`id` int unsigned not null auto_increment primary key,
`order_number` varchar(255) not null, `user_id` int unsigned not null, `status`
enum('pending', 'processing', 'completed', 'decline') not null default 'pending',
`grand_total` decimal(20, 6) not null, `item_count` int unsigned not null, `payment_status`
tinyint(1) not null default '1', `payment_method` varchar(255) null, `first_name`
varchar(255) not null, `last_name` varchar(255) not null, `address` text not null, `city`
varchar(255) not null, `country` varchar(255) not null, `post_code` varchar(255) not null,
`phone_number` varchar(255) not null, `notes` text null, `created_at` timestamp null,
`updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
2022_08_26_082806_create_order_items_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrderItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('order_items');
Schema::create('order_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('order_id')->index();
$table->unsignedBigInteger('product_id')->index();
$table->unsignedInteger('quantity');
$table->decimal('price', 20, 6);
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_items');
}
}
2022_08_26_082629_create_orders_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->string('order_number')->unique();
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->enum('status', ['pending', 'processing', 'completed', 'decline'])->default('pending');
$table->decimal('grand_total', 20, 6);
$table->unsignedInteger('item_count');
$table->boolean('payment_status')->default(1);
$table->string('payment_method')->nullable();
$table->string('first_name');
$table->string('last_name');
$table->text('address');
$table->string('city');
$table->string('country');
$table->string('post_code');
$table->string('phone_number');
$table->text('notes')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
I checked my database and there is 'orders' table but not 'order_items'. Please help me someone!