如何在 Rule::unique() 中使用 whereHas() 方法进行多列唯一验证

问题描述 投票:0回答:1

我可以在 Rule::unique() 中使用 whereHas 方法吗? 这里没有cows的列,奶牛的数据通过多对多关系来自数据透视表。 请帮助我。

我想要这样的东西:

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')],

        ];
    }
php laravel rules
1个回答
0
投票

通过自定义规则解决。这是自定义规则类的代码:

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Models\Vaccine;

class UniqueVaccine implements Rule
{
    protected $request;

    public function __construct($request)
    {
        $this->request = $request;
    }

    public function passes($attribute, $value)
    {
        $vaccination_date = $this->request['vaccination_date'];
        $next_vaccination_date = $this->request['next_vaccination_date'];
        $vaccine_type = $this->request['vaccine_type'];
        $dose = $this->request['dose'];
        $cows = $this->request['cows'];
        $exists =
        Vaccine::where('vaccination_date', $vaccination_date)
            ->where('next_vaccination_date', $next_vaccination_date)
            ->where('vaccine_type', $vaccine_type)
            ->where('dose', $dose)
            ->where(function ($query) use ($cows) {
                $query->WhereHas('cows', function ($subquery) use ($cows) {
                    $subquery->whereIn('id', $cows);
                });
            })->exists();


        // Return true if the combination is unique, false otherwise
        return !$exists;
    }

    public function message()
    {
        return 'A vaccine with the same combination of information already exists.';
    }
}

这是FormRequest类的rules()方法的代码:

public function rules(): array
    {
        return [
            'vaccination_date' => [
                'required', 'date', 'before:tomorrow', new UniqueVaccine($this->all())
            ],

            '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')],
        ];
    }

如果您对我的代码有任何建议,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.