|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | + |
| 7 | +return new class extends Migration |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Run the migrations. |
| 11 | + */ |
| 12 | + public function up(): void |
| 13 | + { |
| 14 | + if (!Schema::hasTable('orders')) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + Schema::table('orders', function (Blueprint $table) { |
| 19 | + if (!Schema::hasColumn('orders', 'customer_email')) { |
| 20 | + $table->string('customer_email')->nullable(); |
| 21 | + } |
| 22 | + if (!Schema::hasColumn('orders', 'shipping_address')) { |
| 23 | + $table->text('shipping_address')->nullable(); |
| 24 | + } |
| 25 | + if (!Schema::hasColumn('orders', 'payment_method')) { |
| 26 | + $table->string('payment_method')->nullable(); |
| 27 | + } |
| 28 | + if (!Schema::hasColumn('orders', 'shipping_cost')) { |
| 29 | + $table->decimal('shipping_cost', 10, 2)->default(0); |
| 30 | + } |
| 31 | + if (!Schema::hasColumn('orders', 'status')) { |
| 32 | + $table->string('status')->default('pending'); |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Reverse the migrations. |
| 39 | + */ |
| 40 | + public function down(): void |
| 41 | + { |
| 42 | + if (!Schema::hasTable('orders')) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + Schema::table('orders', function (Blueprint $table) { |
| 47 | + $columns = []; |
| 48 | + |
| 49 | + if (Schema::hasColumn('orders', 'customer_email')) { |
| 50 | + $columns[] = 'customer_email'; |
| 51 | + } |
| 52 | + if (Schema::hasColumn('orders', 'shipping_address')) { |
| 53 | + $columns[] = 'shipping_address'; |
| 54 | + } |
| 55 | + if (Schema::hasColumn('orders', 'payment_method')) { |
| 56 | + $columns[] = 'payment_method'; |
| 57 | + } |
| 58 | + if (Schema::hasColumn('orders', 'shipping_cost')) { |
| 59 | + $columns[] = 'shipping_cost'; |
| 60 | + } |
| 61 | + if (Schema::hasColumn('orders', 'status')) { |
| 62 | + $columns[] = 'status'; |
| 63 | + } |
| 64 | + |
| 65 | + if (!empty($columns)) { |
| 66 | + $table->dropColumn($columns); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | +}; |
0 commit comments