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
2
  • possible duplicate: stackoverflow.com/questions/50349775/… Commented Nov 28, 2022 at 1: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, Commented Nov 28, 2022 at 2:32

1 Answer 1

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')))
    ]
]);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks before, but that not mean different value from formId, it can same value.
you mean that formId and kecamatanId must have a same value?
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
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
You can try my edited answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.