diff options
| author | Sam Light <samlight1994@gmail.com> | 2026-02-10 00:21:10 +0000 |
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2026-02-10 00:21:10 +0000 |
| commit | ad1c06ee1819b3512e2443d5cbe06199a0ae9bad (patch) | |
| tree | b873596f86173c70c29decc301397d716ad7ca8d /workbench/database | |
| parent | 47c3c4b4e772c30dac12169300e20346fa47b71f (diff) | |
Created order tables and models
Diffstat (limited to 'workbench/database')
| -rw-r--r-- | workbench/database/factories/OrderFactory.php | 35 | ||||
| -rw-r--r-- | workbench/database/migrations/2025_04_01_000000_create_orders_table.php | 40 | ||||
| -rw-r--r-- | workbench/database/seeders/DatabaseSeeder.php | 15 |
3 files changed, 89 insertions, 1 deletions
diff --git a/workbench/database/factories/OrderFactory.php b/workbench/database/factories/OrderFactory.php new file mode 100644 index 0000000..4a48a63 --- /dev/null +++ b/workbench/database/factories/OrderFactory.php @@ -0,0 +1,35 @@ +<?php + +namespace Workbench\Database\Factories; + +use Illuminate\Database\Eloquent\Factories\Factory; +use Workbench\App\Enums\OrderStatus; +use Workbench\App\Models\Order; + +/** + * @template TModel of \Workbench\App\Models\Order + * + * @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel> + */ +class OrderFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string<TModel> + */ + protected $model = Order::class; + + /** + * Define the model's default state. + * + * @return array<string, mixed> + */ + public function definition(): array + { + return [ + 'status' => fake()->randomElement(OrderStatus::cases()), + 'total' => 0, + ]; + } +} diff --git a/workbench/database/migrations/2025_04_01_000000_create_orders_table.php b/workbench/database/migrations/2025_04_01_000000_create_orders_table.php new file mode 100644 index 0000000..838349c --- /dev/null +++ b/workbench/database/migrations/2025_04_01_000000_create_orders_table.php @@ -0,0 +1,40 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; +use Workbench\App\Enums\OrderStatus; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + Schema::create('orders', function (Blueprint $table) { + $table->id(); + $table->enum('status', OrderStatus::cases()); + $table->decimal('total', total: 10, places: 2); + $table->timestamps(); + }); + + Schema::create('order_product', function (Blueprint $table) { + $table->id(); + $table->foreignId('order_id')->constrained()->cascadeOnDelete(); + $table->foreignId('product_id')->constrained()->cascadeOnDelete(); + $table->unsignedInteger('quantity'); + $table->decimal('price', total: 8, places: 2); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('order_product'); + Schema::dropIfExists('orders'); + } +}; diff --git a/workbench/database/seeders/DatabaseSeeder.php b/workbench/database/seeders/DatabaseSeeder.php index 9b7e84a..5dddc62 100644 --- a/workbench/database/seeders/DatabaseSeeder.php +++ b/workbench/database/seeders/DatabaseSeeder.php @@ -4,6 +4,7 @@ namespace Workbench\Database\Seeders; use Illuminate\Database\Seeder; use Workbench\App\Models\Category; +use Workbench\App\Models\Order; use Workbench\App\Models\Product; class DatabaseSeeder extends Seeder @@ -15,9 +16,21 @@ class DatabaseSeeder extends Seeder { $categories = Category::factory()->count(10)->create(); - Product::factory() + $products = Product::factory() ->state(fn () => ['category_id' => $categories->random()->id]) ->count(104) ->create(); + + Order::factory()->count(50)->create()->each(function ($order) use ($products) { + $orderProducts = $products->random(rand(1, 5)); + foreach ($orderProducts as $product) { + $quantity = rand(1, 5); + $order->products()->attach($product->id, [ + 'quantity' => $quantity, + 'price' => $product->price, + ]); + } + $order->update(['total' => $order->products->sum(fn ($p) => $p->pivot->quantity * $p->pivot->price)]); + }); } } |
