init
This commit is contained in:
41
app/Http/Requests/Admin/CategoryRequest.php
Normal file
41
app/Http/Requests/Admin/CategoryRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CategoryRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$categoryId = $this->route('category')?->id;
|
||||
|
||||
return [
|
||||
'type' => ['required', Rule::in($this->typeOptions())],
|
||||
'name' => ['required', 'string', 'max:120'],
|
||||
'slug' => ['required', 'string', 'max:190', Rule::unique('categories', 'slug')->ignore($categoryId)],
|
||||
'description' => ['nullable', 'string'],
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user