Replaces the laravel-default scaffold seeder with one file per domain (AdminUser, User, Post, Comment) plus a config/seed.php to source admin credentials from env. DatabaseSeeder is now a pure orchestrator that calls the sub-seeders in dependency order.
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Post\CreatePostDto;
|
|
use App\Post\PostRepository;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\UserRepository;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class PostSeeder extends Seeder
|
|
{
|
|
public function __construct(
|
|
private PostRepository $postRepo,
|
|
private UserRepository $userRepo,
|
|
) {}
|
|
|
|
public function run(): void
|
|
{
|
|
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
|
|
$samples = [
|
|
[
|
|
'email' => 'alice@example.com',
|
|
'title' => 'Hello World',
|
|
'body' => 'My first post on TIDE.',
|
|
],
|
|
[
|
|
'email' => 'bob@example.com',
|
|
'title' => 'Riding the wave',
|
|
'body' => 'Bob shares his thoughts.',
|
|
],
|
|
[
|
|
'email' => 'carol@example.com',
|
|
'title' => 'Notes from week one',
|
|
'body' => 'Carol lays out her plan.',
|
|
],
|
|
];
|
|
foreach ($samples as $index => $sample) {
|
|
$author = $this->userRepo->findByEmail(
|
|
new EmailAddress($sample['email']),
|
|
);
|
|
if ($author === null) {
|
|
continue;
|
|
}
|
|
$this->postRepo->create(new CreatePostDto(
|
|
userId: $author->getId(),
|
|
title: $sample['title'],
|
|
body: $sample['body'],
|
|
createdAt: $now->modify("-{$index} hour"),
|
|
));
|
|
}
|
|
}
|
|
}
|