Show Error Message Using Helper Function in Laravel

Hello show error message using helper function laravel is the todays topic which i am going to discuss. In this example i will show you how we can show error message in laravel by using our own custom message.

We show error message in our blade file. So in this example i will create a global helper method and we will access it where you want. No need to call laravel error method to show error message.

How we can do that? see the below tutorial to know that how we can show custom error message in laravel using helper method.

Helper.php

if (! function_exists('hasError')) {
    /**
     * Check for the existence of an error message and return a class name
     *
     * @param  string  $key
     * @return string
     */
    function hasError($key)
    {
        $errors = session()->get('errors') ?: new \Illuminate\Support\ViewErrorBag;
        return $errors->has($key) ? 'is-invalid' : '';
    }
}

 

So now we can call it in blade file like that

 

See this feels much cleaner to me, isn't ? Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js