-
database/factories/PostFactory.php
Open in GitHubuse App\Models\Post; use Carbon\CarbonImmutable; use Illuminate\Database\Eloquent\Factories\Factory; class PostFactory extends Factory { public function definition(): array { return [ 'slug' => $this->faker->unique()->slug, 'title' => $this->faker->words(5, true), 'excerpt' => $this->faker->text(160), 'content' => $this->faker->text(500), 'published_at' => $publishedAt = new CarbonImmutable($this->faker->dateTimeBetween('-3 years', 'now')), // Posted between 3 years ago and now. 'created_at' => $this->faker->dateTimeBetween($publishedAt->subDays(7), $publishedAt), // Created between 7 days before publishing and the day of publication. 'updated_at' => $this->faker->randomElement([ // Never updated, or on a day between publication date and a month later. $publishedAt, $this->faker->dateTimeBetween($publishedAt, $publishedAt->addDays(30)), ]), ]; } }
-
database/seeders/PostSeeder.php
Open in GitHubuse App\Models\Post; use Illuminate\Database\Seeder; class PostSeeder extends Seeder { public function run() { Post::factory()->times(30)->create(); } }