Laravel 8 Custom Helper function PHP Fatal error: Cannot redeclare functionName() previously declared in C:(patth)Helpers.php [duplicate]

Solution 1:

You can bypass this error by checking if your function already exists:

if(! function_exists('CheckInvalidPlan')) {
    function CheckInvalidPlan($id)
    {
        if (Plan::find($id) == null)
        {
            return true;
        }
    }
}

That's how Laravel helpers are declared:

if (! function_exists('today')) {
    /**
     * Create a new Carbon instance for the current date.
     *
     * @param  \DateTimeZone|string|null  $tz
     * @return \Illuminate\Support\Carbon
     */
    function today($tz = null)
    {
        return Date::today($tz);
    }
}

However, a cleaner approach would be to understand why your helpers file is loaded twice.

It is hard to tell you exacly where the error could be, however you should inspect all your classes, the app\Helpers.php file should never be required manually. It should be autoloaded by composer, as explained in this answer (thanks N69S).