How to validation with custom message in controller of Laravel

There are lots of tutorials for sending custom messages in validation but I always like the simple way to finish that today I will show you how to send a custom message in Laravel validation by the controller.

ExampleController.php
public function storeUpdate(Request $request)
    {
        // validator
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'age' => 'required',
        ], [
            'name.required' => 'Please insert your name.',
            'age.required' => 'Please enter your age.',
        ]);
        if ($validator->fails()) {
        		// just redirest wich blade file you want to show
            return redirect()->route('user.profile.index')->with('error', $validator->errors()->first());
        }
    }

In the blade file, you will show the error like this,

@if (session('error'))
    <div class="alert alert-dismissible" role="alert">
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        <div class="alert-message">
            <span>{{ session('error') }}</span>
        </div>
    </div>
@endif