ldc-crm/app/Http/Requests/Api/V4/StoreCustomerAnswers.php

49 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Requests\Api\V4;
use App\Models\Answer;
use App\Models\Question;
use App\Models\Service;
class StoreCustomerAnswers extends BaseApiRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
// * Resolve Service.
$service = app()->make(Service::class);
$brandId = $service->brand->id;
$question = Question::query()->findOrFail($this->question);
$questionTableName = (new Question())->getTable();
$answerTableName = (new Answer())->getTable();
return [
'question' => [
'required',
"exists:$questionTableName,id",
2024-09-09 15:26:35 +02:00
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) use ($question, $brandId) {
if ($question->brand->id !== $brandId) {
$fail("$attribute does not belong to brand $brandId");
}
}
],
'answers' => 'required|array',
'answers.*' => [
'int',
'distinct',
"exists:$answerTableName,id",
function ($attribute, $value, $fail) use ($question) {
if (Answer::query()->findOrFail($value)->question->id !== $question->id) {
$fail("$attribute does not belong to question $question->id");
}
}
]
];
}
}