Files

78 lines
2.0 KiB
PHP
Executable File

<?php
namespace App\Http\Requests\Dashboard\Service;
use App\Models\Service;
use Illuminate\Foundation\Http\FormRequest;
class Store extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name_uz' => 'required|string',
'name_ru' => 'required|string',
'image' => 'required|image',
'status' => 'required|in:published,new,soon',
'position' => 'nullable|numeric',
'is_power' => 'nullable|boolean',
'with_power' => 'nullable|boolean',
'problems' => [
'nullable',
'array',
// Only validate the array if 'with_power' is set to true (or 1)
function ($attribute, $value, $fail) {
if ($this->with_power && !is_array($value)) {
$fail($attribute . ' must be an array when with_power is present.');
}
}
],
'problems.*' => [
'nullable',
'exists:problems,id', // Validate each element of the array
]
];
}
/**
* @return array
*/
public function getName(): array
{
return [
'uz' => $this->get('name_uz'),
'ru' => $this->get('name_ru')
];
}
public function getPosition()
{
$position = $this->position;
if ($position == null) {
$service = Service::orderBy('position', 'desc')->first();
if ($service == null) {
return 1;
}
return $service->position + 1;
}
return $position;
}
public function generateSlug()
{
$slug = str_slug($this->name_ru);
$service = Service::where('type', $slug)->first();
if ($service == null) {
return $slug;
}
return $slug . '-' . time();
}
}