Skip to content
Advertisement

How to validate a unique field on a record update

I am recently started a laravel project to control some activities, but I am currently having issues with the validation specifically when updating a specific record on a table that contains unique values

A couple of notes a started this project a couple of days of the launch of laravel 8 last month so perhaps there some changes that are afecting me.

These are my migrations

The first one is a user with its data, …

JavaScript

Second is a table to describe user types …

JavaScript

… An a table to describe user positions …

JavaScript

this is my ususarios controller

JavaScript

And this is the error that I get

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘alias_usuario’ in ‘where clause’ (SQL: select count(*) as aggregate from usuarios where alias_usuario = rytertertcc and id_usuario <> 4)

I checked the official laravel documentation https://laravel.com/docs/8.x/validation#rule-unique so I procceded to implement this Rule::unique(‘usuarios’)->ignore($usuario)] so if I update a user alias that is unique piece of data but no the email or telephone that are also uniques pieces of data I should have not this problem,

The other question related to this Laravel: Validation unique on update has not been useful for this matter.

I really appreciate the help if possible, thank you very much.

Advertisement

Answer

Since the input name doesn’t match the column name, you’ll have to pass the column name when you create the rule. Per the documentation, if your database uses a different column than what you’re passing, then you need to pass along the column name as well:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement