unique laravel code example
Example 1: laravel validator make custom message
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.'
];
$this->validate($request, $rules, $customMessages);
Example 2: laravel unique validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 3: laravel validation
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
Example 4: 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 5: laravel rule unique where
'required|email|unique:company_users,email_address,NULL,id,company_id,' . $request->company_id
Example 6: laravel unique id
function generateBarcodeNumber() {
$number = mt_rand(1000000000, 9999999999);
if (barcodeNumberExists($number)) {
return generateBarcodeNumber();
}
return $number;
}
function barcodeNumberExists($number) {
return User::whereBarcodeNumber($number)->exists();
}