Unique validation
I have a model supporter. I need to when signing up check that a id_number does not exist on supporter and this is fine I get this but I also need to check if the id_number does not exist on members table, because a supporter could be upgraded to member we need to check if the member does not already exist.
so what i have is this
->unique(table: Supporter::class, ignoreRecord: true)
->unique(table: Member::class, ignoreRecord: true)
the supporter works fine but then the member is also looking for supporter id in members table which it does not have
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'supporters.id' in 'where clause'
select
count(*) as aggregate
from
members
where
id_number
= 1234
and supporters
.id
<> 2
how can i get this to work?1 Reply
I think you need to join the supporters table and have one unique rule.
I dont know how to do this exactly because I have not worked much with the unique rule... But it does seem like you can modify the rule:
It says in the doucmentation that it looks for the Model so I would think you can somehow extend the rule with $rule->join('supporters') or something? Not sure though!
the laravel documentation indeed gives you the option to use the Builder:
'email' => Rule::unique('users')->where(fn (Builder $query) => $query->where('account_id', 1))
So I think it is possible to join the supporters table