【问题标题】:Laravel View Composer "Cannot use object of type Closure as array"Laravel View Composer“不能使用闭包类型的对象作为数组”
【发布时间】:2019-07-09 18:02:13
【问题描述】:

我正在为我的视图编辑器使用一个数组,当我尝试查看页面 Cannot use object of type Closure as array 时它会引发此错误。

我尝试使用 eloquent 来代替变量,但最终没有奏效,因为这不是我使用它们的方式应该如何构建的。我还尝试在 $view->with()variable 中创建一个函数。

public function compose(View $view)
    {
        $view->with('configurable', function() {
            $configurables = Cache::rememberForever('configurables', function() {
                return Configurables::all();
            });
            $configurable = [];
            foreach($configurables as $key) {
                $configurable[$key->slug] = [
                    'value' => $key->value
                ];
            }

            return $configurable;
        });
    }

【问题讨论】:

  • 你能显示完整的错误和发生错误的行吗?

标签: php laravel


【解决方案1】:

错误意味着with() 方法需要一个值,而不是函数(闭包)。你可以像下面这样重构你的代码:

public function compose(View $view)
{

        $configurables = Cache::rememberForever('configurables', function() {
            return Configurables::all();
        });

        $configurable = [];

        foreach($configurables as $key) {
            $configurable[$key->slug] = [
                'value' => $key->value
            ];
        }

       $view->with('configurable', $configurable);
}

希望这会有所帮助。

【讨论】:

  • 感谢您的回复!今天早上我会检查它是否有效
  • 感谢您的解决!
  • 很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2019-12-07
  • 2019-12-11
  • 1970-01-01
  • 2018-04-12
  • 2014-07-29
  • 2021-12-17
  • 2019-12-01
相关资源
最近更新 更多