Files
omg-wtf-ai/app/Http/Controllers/ToolController.php
T
2026-08-08 18:03:29 +03:00

90 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Tool;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class ToolController extends Controller
{
public function index(Request $request): View
{
$category = $request->string('category')->toString();
$search = $request->string('q')->toString();
$tools = Tool::approved()
->when($category, fn ($query) => $query->where('category', $category))
->when($search, fn ($query) => $query->where('name', 'like', "%{$search}%"))
->orderByDesc('featured')
->orderByDesc('views')
->paginate(12)
->withQueryString();
return view('tools.index', [
'tools' => $tools,
'categories' => config('tools.categories'),
'activeCategory' => $category,
'search' => $search,
'title' => 'AI Tools Directory — '.config('app.name'),
'description' => 'Discover AI tools for developers, designers, marketers, and more.',
'canonical' => route('tools.index'),
]);
}
public function category(string $category): View
{
abort_unless(array_key_exists($category, config('tools.categories')), 404);
$tools = Tool::approved()
->where('category', $category)
->orderByDesc('featured')
->orderByDesc('views')
->paginate(12);
$label = config("tools.categories.{$category}");
return view('tools.category', [
'tools' => $tools,
'categories' => config('tools.categories'),
'category' => $category,
'label' => $label,
'title' => $label.' AI Tools — '.config('app.name'),
'description' => "AI tools for {$label}.",
'canonical' => route('tools.category', $category),
]);
}
public function show(Tool $tool): View
{
abort_unless($tool->approved, 404);
$tool->increment('views');
$related = Tool::approved()
->where('category', $tool->category)
->whereKeyNot($tool->id)
->limit(4)
->get();
return view('tools.show', [
'tool' => $tool,
'related' => $related,
'title' => $tool->name.' — AI Tools — '.config('app.name'),
'description' => str($tool->description)->limit(160)->toString(),
'canonical' => route('tools.show', $tool),
'image' => $tool->logo_url,
]);
}
public function visit(Tool $tool): RedirectResponse
{
abort_unless($tool->approved, 404);
$tool->increment('clicks');
return redirect()->away($tool->url);
}
}