summaryrefslogtreecommitdiff
path: root/workbench
diff options
context:
space:
mode:
Diffstat (limited to 'workbench')
-rw-r--r--workbench/app/Enums/OrderStatus.php11
-rw-r--r--workbench/app/Models/Order.php51
-rw-r--r--workbench/database/factories/OrderFactory.php35
-rw-r--r--workbench/database/migrations/2025_04_01_000000_create_orders_table.php40
-rw-r--r--workbench/database/seeders/DatabaseSeeder.php15
5 files changed, 151 insertions, 1 deletions
diff --git a/workbench/app/Enums/OrderStatus.php b/workbench/app/Enums/OrderStatus.php
new file mode 100644
index 0000000..555e31a
--- /dev/null
+++ b/workbench/app/Enums/OrderStatus.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Workbench\App\Enums;
+
+enum OrderStatus: string
+{
+ case Pending = 'pending';
+ case Processing = 'processing';
+ case Completed = 'completed';
+ case Cancelled = 'cancelled';
+}
diff --git a/workbench/app/Models/Order.php b/workbench/app/Models/Order.php
new file mode 100644
index 0000000..84a13ab
--- /dev/null
+++ b/workbench/app/Models/Order.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Workbench\App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Support\Carbon;
+use Workbench\App\Enums\OrderStatus;
+use Workbench\Database\Factories\OrderFactory;
+
+/**
+ * @property int $id
+ * @property OrderStatus $status
+ * @property string $total
+ * @property Carbon|null $created_at
+ * @property Carbon|null $updated_at
+ */
+class Order extends Model
+{
+ /**
+ * @use HasFactory<OrderFactory>
+ */
+ use HasFactory;
+
+ /**
+ * @return OrderFactory<static>
+ */
+ protected static function newFactory(): OrderFactory
+ {
+ return OrderFactory::new();
+ }
+
+ /**
+ * @return array<string, string>
+ */
+ protected function casts(): array
+ {
+ return [
+ 'status' => OrderStatus::class,
+ ];
+ }
+
+ /**
+ * @return BelongsToMany<Product, $this>
+ */
+ public function products(): BelongsToMany
+ {
+ return $this->belongsToMany(Product::class)->withPivot('quantity', 'price')->withTimestamps();
+ }
+}
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)]);
+ });
}
}