【问题标题】:Undefined variable error after upgrading from 5.3 to 5.8 in laravel在 laravel 中从 5.3 升级到 5.8 后出现未定义变量错误
【发布时间】:2019-12-24 06:19:29
【问题描述】:

我最近刚刚将我的项目从 5.3 升级到了 5.8。但在那之后,我遇到了以下错误:-

这是我控制器中的代码:-

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'));

        }
    }

当我像下面这样更改代码时,错误得到解决,但我问为什么 withCategoriesHierarchy 不起作用。如果我必须改变这一点,那么我必须改变整个项目。所以会很麻烦。所以我正在寻找一些解决方案。任何帮助将不胜感激。提前致谢。


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'));

【问题讨论】:

  • 你试过->withCategoriesHierarchy($hierarchy)而不是->with('categories_hierarchy',$hierarchy)吗? . view:clear 怎么样?
  • @danish-khan-我是的,这是有效的,但我在问为什么 withCategoriesHierarchy 不起作用??...因为我必须更改相同的在整个项目中多次发生。是的,我已经清除了各种缓存。

标签: php laravel-blade laravel-5.8


【解决方案1】:

自 5.8 版以来,Laravel 更改了 Illuminate\View 上 __call 魔术函数的实现

旧功能:

    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.");
}

新功能

    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]);
}

看起来 static::hasMacro($method) 出于某种原因返回 true (需要调试) 然后调用 macroCall 而不是 $this->with(Str::camel(substr($method, 4)), $parameters[0]);

如果你想检查它,只需将新功能更改为旧功能 (当然只是为了测试)如果它可以工作,请更深入地调试它以了解那里发生了什么。

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2019-12-05
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    相关资源
    最近更新 更多