fix: limit articles shown at homepage to published articles only

This also improves the published_at generation logic for articles
within the ArticleFactory.
This commit is contained in:
Jonathan Nielson 2025-03-19 16:52:49 -05:00
parent 62a9279603
commit a9155b413f
2 changed files with 13 additions and 2 deletions

View File

@ -4,6 +4,7 @@ namespace Database\Factories;
use App\Models\Author; use App\Models\Author;
use App\Models\Article; use App\Models\Article;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
/** /**
@ -46,7 +47,7 @@ class ArticleFactory extends Factory
'author_id' => static::$author->id ??= Author::factory()->create()->id, 'author_id' => static::$author->id ??= Author::factory()->create()->id,
'subtitle' => fake()->sentence(), 'subtitle' => fake()->sentence(),
'body' => '', // We'll update it after making using the on-Model method '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(), 'header_image_url' => fake()->imageUrl(),
'markdown_body' => $this->generateMarkdown(), 'markdown_body' => $this->generateMarkdown(),
'processed_at' => fake()->date('Y-m-d H:m:s'), 'processed_at' => fake()->date('Y-m-d H:m:s'),
@ -87,4 +88,14 @@ class ArticleFactory extends Factory
return $markdown_result; 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));
}
}
} }

View File

@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Route;
*/ */
Route::get('/', function () { 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]); return view('guest.home', ['articles' => $articles]);
}); });