Can I use whereHas method in Rule::unique() ? There are no column here for cows, the data of cows are comming from pivot table by ManyToMany relationship. Please help me.
I want something like that:
public function rules(): array
{
return [
'vaccination_date' => [
'required', 'date', 'before:tomorrow',
Rule::unique('vaccines', 'vaccination_date')
->where(function ($query) {
$query->where('vaccination_date', $this->input('vaccination_date'))
->where('next_vaccination_date', $this->input('next_vaccination_date'))
->where('vaccine_type', $this->input('vaccine_type'))
->where('dose', $this->input('dose'))
->whereHas('cows', function($query) {
$query->whereIn('id', [1,2]);
})->get();
}),],
'next_vaccination_date' => ['required', 'date', 'after:vaccination_date'],
'vaccine_type' => [
'required',
'string',
new Enum(VaccineTypeEnum::class)
],
'dose' => [
'required',
'string',
new Enum(VaccineDoseEnum::class)
],
'cows' => ['required', 'array', 'min:1', Rule::exists('cows', 'id')],
];
}
Solved with custom rules. Here is the code of the custom Rule class:
Here is the code of rules() method of the FormRequest class:
Let me know if you have any suggestions about my code.