Relationship Unique Validation #579
-
Hi everyone,
In the setupUpdateOperation method I added a field with type: relationship and the subfields.
In my UserUpdateCrudRequest I have these rules:
The issue here is in the $id variable, because I'm editing a user object and $id will be related to the editing user. How can I grab the relationship code $id to ignore it inside the unique rule? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @iAdil First of all, I'm not experienced to use the relationship field type, but what I got from your question I can say that, we can try these steps
//inside UserUpdateCrudRequest
$id = $this->get('id') ?? request()->route('id');
$getCodes = User::with('codes')->where('id', $id)->get()->pluck('codes.id', 'id');
//inside UserUpdateCrudRequest , also don't forget to import the Rule Validation Class
use Illuminate\Validation\Rule; // put this
$id = $this->get('id') ?? request()->route('id');
$getCodesId = User::with('codes')->where('id', $id)->get()->pluck('codes.id', 'id');
return [
'codes.*.number' => [
'min:3',
'required',
'distinct',
Rule::unique('codes', 'number')->where(function ($query) use ($getCodesId) {
$query->whereNotIn('id', $getCodesId);
return $query;
})
],
]; Reference Let me know how is it, if you have any issues let me know mate. Cheers. |
Beta Was this translation helpful? Give feedback.
Hi @iAdil
First of all, I'm not experienced to use the relationship field type, but what I got from your question I can say that, we can try these steps