blob: 5dddc623c2fc091f80713533d9e8bf5735656289 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php
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
{
/**
* Seed the application's database.
*/
public function run(): void
{
$categories = Category::factory()->count(10)->create();
$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)]);
});
}
}
|