66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Category;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
$categories = Category::orderBy('type')->orderBy('sort')->orderBy('name')->paginate(20);
|
|
|
|
return view('admin.categories.index', compact('categories'));
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.categories.form', ['category' => null]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:60'],
|
|
'slug' => ['required', 'string', 'max:80', 'unique:categories,slug'],
|
|
'type' => ['required', 'in:product,article'],
|
|
'parent_id' => ['nullable', 'integer'],
|
|
'sort' => ['integer'],
|
|
]);
|
|
|
|
Category::create($data);
|
|
|
|
return redirect()->route('admin.categories.index')->with('success', '分类已创建');
|
|
}
|
|
|
|
public function edit(Category $category): View
|
|
{
|
|
return view('admin.categories.form', compact('category'));
|
|
}
|
|
|
|
public function update(Request $request, Category $category)
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:60'],
|
|
'slug' => ['required', 'string', 'max:80', 'unique:categories,slug,' . $category->id],
|
|
'type' => ['required', 'in:product,article'],
|
|
'parent_id' => ['nullable', 'integer'],
|
|
'sort' => ['integer'],
|
|
]);
|
|
|
|
$category->update($data);
|
|
|
|
return redirect()->route('admin.categories.index')->with('success', '分类已更新');
|
|
}
|
|
|
|
public function destroy(Category $category)
|
|
{
|
|
$category->delete();
|
|
|
|
return redirect()->route('admin.categories.index')->with('success', '分类已删除');
|
|
}
|
|
}
|