laravel unique validation with condition code example
Example 1: laravel unique validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 2: set unique value validation for laravel form request
use Illuminate\Validation\Rule;
public function rules()
{
return [
'title' => [
'required',
Rule::unique('posts', 'title')->ignore($this->post)
]
];
}
Example 3: laravel unique validation on multiple columns
'mobile_no'
=> 'unique:users,mobile_no,NULL,id,country_id,'.request('country_id');
Example 4: laravel validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 5: laravel unique validation
unique:table,column,except,idColumn
############## Example : ################
############### For Updating
'email' => 'unique:users,email_address,' . $userId,
############### For Creating
'email' => 'unique:users,email_address',
Example 6: laravel rule unique where
'required|email|unique:company_users,email_address,NULL,id,company_id,' . $request->company_id