92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Admin\CategoryRequest;
|
||
|
|
use App\Models\Category;
|
||
|
|
use Illuminate\Http\RedirectResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\View\View;
|
||
|
|
|
||
|
|
class CategoryController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Request $request): View
|
||
|
|
{
|
||
|
|
$items = Category::query()
|
||
|
|
->when($request->filled('q'), function ($query) use ($request): void {
|
||
|
|
$keyword = (string) $request->string('q');
|
||
|
|
$query->where('name', 'like', "%{$keyword}%")
|
||
|
|
->orWhere('slug', 'like', "%{$keyword}%")
|
||
|
|
->orWhere('description', 'like', "%{$keyword}%");
|
||
|
|
})
|
||
|
|
->when($request->filled('type'), function ($query) use ($request): void {
|
||
|
|
$query->where('type', (string) $request->string('type'));
|
||
|
|
})
|
||
|
|
->orderBy('type')
|
||
|
|
->orderBy('name')
|
||
|
|
->paginate(30)
|
||
|
|
->withQueryString();
|
||
|
|
|
||
|
|
return view('admin.categories.index', [
|
||
|
|
'items' => $items,
|
||
|
|
'filters' => $request->only(['q', 'type']),
|
||
|
|
'typeOptions' => $this->typeOptions(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(): View
|
||
|
|
{
|
||
|
|
return view('admin.categories.form', [
|
||
|
|
'item' => new Category(),
|
||
|
|
'submitRoute' => route('admin.categories.store'),
|
||
|
|
'method' => 'POST',
|
||
|
|
'typeOptions' => $this->typeOptions(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(CategoryRequest $request): RedirectResponse
|
||
|
|
{
|
||
|
|
$data = $request->validated();
|
||
|
|
$data['is_active'] = (bool) ($data['is_active'] ?? false);
|
||
|
|
|
||
|
|
$category = Category::query()->create($data);
|
||
|
|
|
||
|
|
return redirect()->route('admin.categories.edit', $category)->with('status', '分类已创建');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(Category $category): View
|
||
|
|
{
|
||
|
|
return view('admin.categories.form', [
|
||
|
|
'item' => $category,
|
||
|
|
'submitRoute' => route('admin.categories.update', $category),
|
||
|
|
'method' => 'PUT',
|
||
|
|
'typeOptions' => $this->typeOptions(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(CategoryRequest $request, Category $category): RedirectResponse
|
||
|
|
{
|
||
|
|
$data = $request->validated();
|
||
|
|
$data['is_active'] = (bool) ($data['is_active'] ?? false);
|
||
|
|
|
||
|
|
$category->fill($data);
|
||
|
|
$category->save();
|
||
|
|
|
||
|
|
return redirect()->route('admin.categories.edit', $category)->with('status', '分类已更新');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<int, string>
|
||
|
|
*/
|
||
|
|
private function typeOptions(): array
|
||
|
|
{
|
||
|
|
$defaults = ['tool', 'model', 'news', 'guide'];
|
||
|
|
$existing = Category::query()->distinct()->pluck('type')->filter()->values()->all();
|
||
|
|
|
||
|
|
return array_values(array_unique([...$defaults, ...$existing]));
|
||
|
|
}
|
||
|
|
}
|