Files
fitness/app/Http/Controllers/BlogController.php
T
rootandClaude Sonnet 5 4686dea609 Add blog feature and fix broken test/build infra
BlogController was an empty stub with no view, so /blog 500'd on every
visit. Adds a real Post model, public listing/detail pages, and an admin
CRUD screen mirroring the existing Exercise admin pattern (image upload
or URL, auto-slug, publish toggle). Seeds 5 real posts so it's not empty
on launch.

Also fixes two things that were silently broken: phpunit.xml pointed
DB_DATABASE at a non-existent "testing" file with no DB_CONNECTION set,
and the stock ExampleTest had RefreshDatabase commented out - both meant
the test suite never actually ran successfully.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-10 11:36:31 +03:00

50 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\View\View;
class BlogController extends Controller
{
public function index(Request $request): View
{
$q = trim((string) $request->query('q', ''));
$posts = Post::query()
->published()
->with('author')
->when($q !== '', function ($qb) use ($q) {
$qb->where(function ($sub) use ($q) {
$sub->where('title', 'like', "%{$q}%")
->orWhere('excerpt', 'like', "%{$q}%");
});
})
->latest('published_at')
->paginate(9)
->withQueryString();
return view('blog.index', [
'posts' => $posts,
'filters' => ['q' => $q],
]);
}
public function show(Post $post): View
{
abort_unless($post->is_published, 404);
$post->load('author');
$relatedPosts = Post::query()
->published()
->where('id', '!=', $post->id)
->latest('published_at')
->limit(3)
->get();
return view('blog.show', compact('post', 'relatedPosts'));
}
}