For example, I have 2 tables : sites1
and sites2
I need to check that field url
which is comes from my html form is unique.
Here my validation rule :
public function rules()
{
return [
'url' => unique:sites1|unique:sites2'
];
}
Unfortunately, this rule applies only for sites2
table. Is there any possible ways to validate both tables?
Your validation rule seems ok. Just make sure that both sites1
and sites2
table has field name url
and both in same database.
Your unique:sites1
rule will be translated into SQL
select count(*) as aggregate from `sites1` where `url` = ?
While unique:sites2
rule will be translated into SQL
select count(*) as aggregate from `sites2` where `url` = ?
See if first SQL does return result. Long URL may result non unique if used with limited index length. It may be better if you could store hash value of URL so you can compare url just by using hash.
No need to maintain the same name in two different tables
In laravel 4.2
Validator::make(Input::all, [
'url' => 'unique:site1,your_column_name|unique:site2:your_column_name_2'
]);
Laravel 5.*:
$this->validate($request,[
'url' => 'unique:site1,your_column_name|unique:site2:your_column_name_2'
]);
Hopefully it's working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With