laravel custom validation rule change attribute name code example

Example 1: custom rule laravel validation

public function store()
{
    $this->validate(request(), [
        'song' => [function ($attribute, $value, $fail) {
            if ($value <= 10) {
                $fail(':attribute needs more cowbell!');
            }
        }]
    ]);
}

Example 2: add custom attribute for validation errors laravel

# add custom attribute for validation errors laravel 8
$rules = [
  'account_number' => ['required','digits:10','max:10','unique:bank_details']
];
$messages = [];
$attributes = [
    'account_number' => 'Mobile number',
];
$request->validate($rules,$messages,$attributes);
// OR
$validator = Validator::make($request->all(), $rules, $messages, $attributes);

Tags:

Php Example