Issue
I've a Custom function in App\Helpers.php. When I use the function in if statement in a blade file. I see error in Laragon error logs.
PHP Fatal error: Cannot redeclare CheckInvalidPlan() (previously declared in C:\laragon\www\projectname\app\Helpers.php:6) in C:\laragon\www\projectname\app\Helpers.php on line 6
However things works as expected. But why is this error causing and how can I fix it?
#Update
This is my function in Helpers.php
function CheckInvalidPlan($id)
{
if (Plan::find($id) == null)
{
return true;
}
}
This is my if statement in Controller.
if (CheckInvalidPlan ($request->plan_id))
{
return back()->with('invalid', 'Invalid membership plan spesified.');
}
Solution
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).
Answered By - Anthony Aslangul
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.