I have a view in Laravel 8 that manages Vendor data, it has a dropdown field whose content is a list of users, what I need to do is validate that each Vendor has a user associated with it, that is, that there is a 1 to 1 relationship between vendors and users. Now, what I don't know is how to validate that a user is not assigned to a seller when recording the seller's data. I have the other form validations running, but I can't figure out how to do the aforementioned validation.
In the image I show you this dropdown that is loaded with the user data, and at the time of recording, you should verify that the selected user is not associated with a seller, otherwise, it would not be respecting the established 1 to 1 relationship for users and sellers.
I am doing the other validations in the Vendors Controller:
/**
* @param Request $request
*/
private function validarCampos(Request $request): void
{
$request->validate([
'id_usuario' => 'required',
'nombre' => 'required|max:100',
'telCelular' => 'required',
'ctaEmail' => 'required',
'avatar'=>'mimes:jpg,jpeg,png,gif,svg,webp|max:2048'
],
[
'id_usuario.required'=>'Debe seleccionar un Usuario de la Lista de Usuarios',
'nombre.required'=>'Debe ingresar un Nombre',
'nombre.max'=>'Cantidad máxima de caracteres permitidos = 100',
'telCelular.required'=>'Debe ingresar un número de teléfono celular',
'ctaEmail.required'=>'Debe ingresar una cuenta de correo electrónico válida',
'avatar.mimes'=>'Debe seleccionar un archivo de imágen',
'avatar.max'=>'El archivo de imagen pesa mas de 2Mb'
]);
}
and this is the procedure to validate within the Controller:
/**
* @param Request $request
*/
private function validarCampos(Request $request): void
{
$request->validate([
'id_usuario' => 'required',
'nombre' => 'required|max:100',
'telCelular' => 'required',
'ctaEmail' => 'required',
'avatar'=>'mimes:jpg,jpeg,png,gif,svg,webp|max:2048'
],
[
'id_usuario.required'=>'Debe seleccionar un Usuario de la Lista de Usuarios',
'nombre.required'=>'Debe ingresar un Nombre',
'nombre.max'=>'Cantidad máxima de caracteres permitidos = 100',
'telCelular.required'=>'Debe ingresar un número de teléfono celular',
'ctaEmail.required'=>'Debe ingresar una cuenta de correo electrónico válida',
'avatar.mimes'=>'Debe seleccionar un archivo de imágen',
'avatar.max'=>'El archivo de imagen pesa mas de 2Mb'
]);
}
Greetings and until another contact.
Something that occurs to me, is using the validation rule
in
.So to that rule you should pass an array with the ids that are allowed, that is, those of the Users that have not yet been assigned to a Seller. For that you can query the absence of the relationship .
For example, if model is called
User
and relationvendedor
, the query would be:Then you pass it to the rule
in
;Or the other way around, and perhaps more efficient, by getting the ids from the Seller model, and using the
notIn
.Although now that I look at it better, I think that the unique rule would be the most appropriate.
I leave the previous ones only as alternatives.
Other references:
Query Builder pluck() .
CollectionstoArray () .
Based on the answers they gave me, I took into consideration the first suggestion of loading the dropdown ONLY with the User records that were not yet linked to any Seller, that way I was able to solve the problem I had, that is, to prevent a user would be linked to more than one seller, which could break the one-to-one relationship that exists between users and sellers. And from the second post, I used the concept of selecting those records that are not within the relationship, that is, those users that have not yet been assigned to a vendor. To do this, I made the implementation inside the Seller model, as shown below:
and then when I need to load the view, I use it from the Seller Controller like this:
finally, this view is called from:
the line $usuarios = $itVendedor->usrNoVinculados(); will return ONLY the list of users that are not yet linked to any seller, this being the goal I was after without breaking the one-to-one relationship between users and sellers.
Greetings and thanks for the contributions that helped me to support this implementation.