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:
parent
62a9279603
commit
a9155b413f
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue