make(\App\Models\Service::class); $brandId = $service->brand->id; $question = Question::findOrFail($this->question); $questionTableName = (new Question())->getTable(); $answerTableName = (new Answer())->getTable(); return [ 'question' => [ 'required', "exists:$questionTableName,id", // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed 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::findOrFail($value)->question->id !== $question->id) { $fail("$attribute does not belong to question $question->id"); } } ] ]; } }