0

I want to do validate when store and update data in Laravel 9. My question is how to do that validate unique more than one field?

I want to store data, that is validate formId and kecamatanId only one data stored in database.

For example:

formId: 1
kecamatanId: 1

if user save the same formId and kecamatanId value, its cant saved, and show the validation message.

But if user save:

formId: 1,
kecamatanId: 2

Its will successfully saved.

And then user save again with:

formId: 1,
kecamatanId: 2

It cant saved, because its already saved with the same condition formId and kecamatanId.

My current validate code:

        $this->validate($request, [
            'formId' => 'required|unique:data_masters',
            'kecamatanId' => 'required',
            'level' => 'required',
            'fieldDatas' => 'required'
        ]);

Update: I have try:


use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$formId = $request->formId;
        $kecamatanId = $request->kecamatanId;

        Validator::make($request, [
            'formId' => [
                'required',
                Rule::unique('data_masters')->where(function ($query) use ($formId, $kecamatanId) {
                    return $query->where('formId', $formId)->where('kecamatanId', $kecamatanId);
                }),
            ],
        ]);

But its return error:

Illuminate\Validation\Factory::make(): Argument #1 ($data) must be of type array, Illuminate\Http\Request given, called in /Volumes/project_name/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 338
ASHafizullah
  • 603
  • 1
  • 9
  • 22
  • possible duplicate: https://stackoverflow.com/questions/50349775/laravel-unique-validation-on-multiple-columns – itachi Nov 28 '22 at 01:58
  • I have try that, but its return error. Maybe not support with laravel 9? ```Illuminate\Validation\Factory::make(): Argument #1 ($data) must be of type array, Illuminate\Http\Request given, ``` – ASHafizullah Nov 28 '22 at 02:32

1 Answers1

0

You can dry using the different:field rule : which validate that the validated field must be different to the given field;

$this->validate($request,[
    'formId' => ['unique:users'],
    'kecamatanId' => [
        'required',
        Rule::unique('users')->where(fn($query) => $query->where('formId', $request->input('formId')))
    ]
]);
xenooooo
  • 1,106
  • 1
  • 2
  • 8
  • Thanks before, but that not mean different value from formId, it can same value. – ASHafizullah Nov 28 '22 at 02:44
  • you mean that `formId` and `kecamatanId` must have a same value? – xenooooo Nov 28 '22 at 02:46
  • for example ```formId = 2, kecamatanId = 1```, and if user save again with ```formId = 2, kecamatanId = 1```, its cant be saved. If user save with ```formId = 2, kecamatanId = 2``` it will successfully saved – ASHafizullah Nov 28 '22 at 02:51
  • after ```formId = 2, kecamatanId = 2``` saved, then user cant save data again with ```formId = 2, kecamatanId = 2```, user can save with ```formId = 2, kecamatanId = 3``` – ASHafizullah Nov 28 '22 at 02:52
  • You can try my edited answer – xenooooo Nov 28 '22 at 03:03