2

UpdateEntityRequest.php:

'phones'          => 'sometimes|nullable|array',
'phones.*.id'     => 'sometimes|required|integer|distinct|exists:entity_phones,id,entity_id,'.$this->id,
'phones.*.number' => 'required|alpha_num|max:255|distinct|unique:entity_phones,number,'.$this->id.',entity_id',

entity_phones table:

id, number, entity_id.
unique constraint: (number, entity_id)

EntityRepository.php:

foreach ($attributes['phones'] as $phone) {
    if (isset($phone['id'])) {
        $entity->phones()->updateOrCreate([
            'id'         => $phone['id'],
            'entity_id'  => $entity->id
        ], $phone);
    } else {
        $entity->phones()->create($phone);
    }
}

My entity can have more than phone associated, but not a repeated number. My intention is to check the unique (entity_id, number) in the UpdateEntityRequest.php so:

  • If the phone object comes without an id, it should check that the combination of number, entity_id doesn't exists. But the number can exist with other entity_id.
  • If the request comes with an id, it should check that the combination of number, entity_id doesn't exists only in other ids, but ignore the given id.

I'm having trouble witht the Laravel Unique rule validating only when i want it to make the validation. Any ideas how could I make this solution would be appreciated.

2 Answers 2

1

If you need to ignore a given ID during the unique check try using the Rule class to fluently define the rule.

use Illuminate\Validation\Rule;

Validator::make($request_data, [
    'number' => [
        'required',
        'alpha_num', (...all your other rules)
        Rule::unique('entity_phones')->ignore($entity_id),
    ],
]);

You can read more in laravel docs about unique rule in paragraph: Forcing A Unique Rule To Ignore A Given ID.

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

Comments

1

I ended up doing this:

$phoneIds = $this->input('phones.*.id');

'phones.*.number' =>
                    [
                        'required_with:phones',
                        'alpha_num',
                        'max:255',
                        'distinct',
                        Rule::unique('entity_phones', 'number')
                        ->where('entity_id', $this->id)
                        ->where(function ($query) use ($phoneIds) {
                            return $query->where('id', '!=', array_shift($phoneIds));
                        })
                    ],

Comments

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.