1

That I want to make validation on an array value, and this is my code:

I have a function that can make a query to another table using hasMany but I want to validate that query must be unique. I tried to use this "required|unique:deed_legalization_numbers, number" and that's work but when I tried to update/edit some field, it will return already taken.

Then i tried to make ignoring ID in unique:

"required|unique:deed_legalization_numbers, number, [$request->input('deed_legalization_numbers')]"

it will return array to string conversion, because not support array

Then I tried this code:

$deed_legalization_numbers = $request->input('deed_legalization_numbers.*.number');
$deed_legalization_id = $request->input('deed_legalization_numbers.*.id');


$rules = [
    'deed_legalization_numbers.*.number' => //'required|unique:deed_legalization_numbers,number,deed_legalization_numbers.*.id',
    [
        'required',
        Rule::unique('deed_legalization_numbers')->where(function ($query) use ($deed_legalization_numbers, $deed_legalization_id) {
            return $query->whereIn('number', $deed_legalization_numbers);
        })->ignore($deed_legalization_id[0]) // The problem in this line
    ],
];

$validator = Validator::make($request->all(), $rules);

if ($validator->fails()) {
    return redirect()->back()
        ->withErrors($validator)
        ->withInput();
}

I want to ignore that deed_legalization_numbers ID from checking, if i changed variable $deed_legalization_id[0] to $deed_legalization_id it will return error :

Status Code: 500 Internal Server Error

Symfony\Component\Debug\Exception\FatalErrorException: Method Illuminate\Validation\Rules\Unique::__toString() must not throw an exception, caught ErrorException: addslashes() expects parameter 1 to be string, array given in file ..path\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php on line 233

0 {main}

2 Answers 2

4

After searching so many threads, then I found the solution to my problem

So, I just change this

$deed_legalization_id = $request->input('deed_legalization_numbers.*.id');

$rules = [
    'deed_legalization_numbers.*.number' => //'required|unique:deed_legalization_numbers,number',
    [
        'required',
        'distinct',
        Rule::unique('deed_legalization_numbers', 'number')->where(function ($query) use ($deed_legalization_id) {
            $query->whereNotIn('id', $deed_legalization_id);
            return $query;
        })
    ],
];

$validator = Validator::make($request->all(), $rules, $message);

if ($validator->fails()) {
    return redirect()->back()
        ->withErrors($validator)
        ->withInput();
}

And everything is just fine, no more duplicate column_number and must be unique

Sign up to request clarification or add additional context in comments.

1 Comment

How to prevent if the input itself contain same number?
1

Your self-answer was helpful. Not sure if it was there 2 years ago, but there is an undocumented helper method called whereNotIn() that allows you to skip the callback stuff. So I would do this for your case:

$deed_legalization_id = $request->input('deed_legalization_numbers.*.id');

$rules = [
    'deed_legalization_numbers.*.number' => [
        'required',
        'distinct',
        Rule::unique('deed_legalization_numbers', 'number')
            ->whereNotIn('id', $deed_legalization_id),
    ],
];

This results in the same database query:

select count(*) as aggregate from `deed_legalization_numbers` where `number` = ? and (`id` not in (?));

4 Comments

but this cause some problem, indeed it exclude the ids array, but what if the array itself contain non unique number: ['id' => 1, 'number' => 123, 'id' => 2, 'number' => 123]
I’m not sure what you’re saying. That is not a valid array structure
[['id' => 1, 'number' => 123], ['id' => 2, 'number' => 123]], this should be nested apologize, imagine this data come from the form, the number field is identical, that will be pass through the validation
I see what you’re saying. The distinct rule takes care of that. laravel.com/docs/11.x/validation#rule-distinct

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.