diff --git a/database/factories/ArticleFactory.php b/database/factories/ArticleFactory.php index ccb51b1..0bac576 100644 --- a/database/factories/ArticleFactory.php +++ b/database/factories/ArticleFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories; use App\Models\Author; use App\Models\Article; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -46,7 +47,7 @@ class ArticleFactory extends Factory 'author_id' => static::$author->id ??= Author::factory()->create()->id, 'subtitle' => fake()->sentence(), 'body' => '', // We'll update it after making using the on-Model method - 'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))), + 'published_at' => $this->generateRandomPublishDate(), 'header_image_url' => fake()->imageUrl(), 'markdown_body' => $this->generateMarkdown(), 'processed_at' => fake()->date('Y-m-d H:m:s'), @@ -87,4 +88,14 @@ class ArticleFactory extends Factory return $markdown_result; } + + private function generateRandomPublishDate(): Carbon + { + $isOlder = rand(0,1); + if($isOlder) { + return now()->subDays(rand(1,800)); + } else { + return now()->addDays(rand(0,50)); + } + } } diff --git a/routes/web.php b/routes/web.php index 1e13af8..a87f3eb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Route; */ Route::get('/', function () { - $articles = Article::latest()->limit(10)->get(); + $articles = Article::where('published_at', '<', now())->latest()->limit(10)->get(); return view('guest.home', ['articles' => $articles]); });