Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “拉威尔视图作曲家”;无法将闭包类型的对象用作数组;_Php_Laravel - Fatal编程技术网

Php “拉威尔视图作曲家”;无法将闭包类型的对象用作数组;

Php “拉威尔视图作曲家”;无法将闭包类型的对象用作数组;,php,laravel,Php,Laravel,我正在为我的view composer使用数组,当我尝试查看页面时,它会抛出此错误无法将闭包类型的对象用作数组 我试着用eloquent代替变量,但最终没有成功,因为这不是我使用它们的方式。我还尝试在$view->with()变量中创建一个函数 public function compose(View $view) { $view->with('configurable', function() { $configurables = Cach

我正在为我的view composer使用数组,当我尝试查看页面时,它会抛出此错误
无法将闭包类型的对象用作数组

我试着用eloquent代替变量,但最终没有成功,因为这不是我使用它们的方式。我还尝试在
$view->with()变量中创建一个函数

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


该错误意味着
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);
}

希望这有帮助。

您能显示完整的错误和错误发生的行吗?谢谢您的回复!今天早上我会检查它是否有效谢谢你,这是修复方法!很高兴这有帮助:)