简体   繁体   中英

Undefined variable error after upgrading from 5.3 to 5.8 in laravel

I just recently upgraded my project from 5.3 to 5.8. but after that, I'm having the following error:-

这是错误的图像

This is the code in my controller:-

public function index()
    {

        $categories = $this->category->getAll();
        $plucked_categories = $this->category->pluckedCollection($categories);
        $hierarchy = $this->category->getCategoriesHierarchy();

        //get through permissions
        if (\Gate::denies('view-categories')) {
            return redirect('/')->withErrors(config('const.permissions_errors.section'));
        } else {
            return view('categories.index')->withCategories($categories)
                                        ->withCategoriesHierarchy($hierarchy)
                                        ->with(compact('plucked_categories'));

        }
    }

When I change the code like this below the error gets solved but I'm asking why withCategoriesHierarchy does not work. If I have to change this then I have to change in the entire project. so that will be troublesome. So I'm looking for some solution. any help will be appreciated. thanks in advance.


return view('categories.index')
->withCategories($categories)
->with(compact('categories_hierarchy','plucked_categories'));

============================
return view('categories.index')
->withCategories($categories)
->with('categories_hierarchy',$hierarchy)
->with(compact('plucked_categories'));

Since version 5.8 Laravel change the implementation of __call magic function on Illuminate\\View

Old function:

    public function __call($method, $parameters)
{
    if (starts_with($method, 'with')) {
        return $this->with(snake_case(substr($method, 4)), $parameters[0]);
    }
    throw new \BadMethodCallException("Method [$method] does not exist on view.");
}

New function

    public function __call($method, $parameters)
{
    if (static::hasMacro($method)) {
        return $this->macroCall($method, $parameters);
    }
    if (! Str::startsWith($method, 'with')) {
        throw new BadMethodCallException(sprintf(
            'Method %s::%s does not exist.', static::class, $method
        ));
    }
    return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
}

Looks like static::hasMacro($method) return true for some reason ( need to debug ) then is called to macroCall and not to $this->with(Str::camel(substr($method, 4)), $parameters[0]);

if you want to check it, just change the new function to old one ( just for testing of course ) if it's work, debug it deeper to understand what is happening there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM