#4-merge-with-master-conflict-resolve

This commit is contained in:
2026-05-12 11:00:22 +08:00
19 changed files with 819 additions and 79 deletions

View File

@@ -0,0 +1,80 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use App\Models\Category;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class CategoryController extends Controller
{
/**
* Display a listing of categories.
*/
public function index(): View
{
$categories = Category::query()
->latest()
->paginate(10, ['id', 'name', 'slug', 'description', 'color', 'created_at']);
return view('category.index', [
'categories' => $categories,
]);
}
/**
* Show the form for creating a new category.
*/
public function create(): View
{
return view('category.create');
}
/**
* Store a newly created category.
*/
public function store(StoreCategoryRequest $request): RedirectResponse
{
Category::query()->create($request->validated());
return redirect()
->route('category.index')
->with('status', 'category-created');
}
/**
* Show the form for editing the specified category.
*/
public function edit(Category $category): View
{
return view('category.edit', [
'category' => $category,
]);
}
/**
* Update the specified category.
*/
public function update(UpdateCategoryRequest $request, Category $category): RedirectResponse
{
$category->update($request->validated());
return redirect()
->route('category.edit', $category)
->with('status', 'category-updated');
}
/**
* Remove the specified category.
*/
public function destroy(Category $category): RedirectResponse
{
$category->delete();
return redirect()
->route('category.index')
->with('status', 'category-deleted');
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class StoreCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'slug' => Str::slug((string) ($this->filled('slug') ? $this->input('slug') : $this->input('name'))),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255', 'unique:categories,name'],
'slug' => ['required', 'string', 'max:255', 'alpha_dash:ascii', 'unique:categories,slug'],
'description' => ['nullable', 'string', 'max:1000'],
'color' => ['required', 'string', 'regex:/^#[0-9A-Fa-f]{6}$/'],
];
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class UpdateCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'slug' => Str::slug((string) ($this->filled('slug') ? $this->input('slug') : $this->input('name'))),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$categoryId = $this->route('category')?->id;
return [
'name' => ['required', 'string', 'max:255', Rule::unique('categories', 'name')->ignore($categoryId)],
'slug' => ['required', 'string', 'max:255', 'alpha_dash:ascii', Rule::unique('categories', 'slug')->ignore($categoryId)],
'description' => ['nullable', 'string', 'max:1000'],
'color' => ['required', 'string', 'regex:/^#[0-9A-Fa-f]{6}$/'],
];
}
}

15
app/Models/Category.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Database\Factories\CategoryFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
#[Fillable(['name', 'slug', 'description', 'color'])]
class Category extends Model
{
/** @use HasFactory<CategoryFactory> */
use HasFactory;
}