我有一个这样的数组:
$currentSelection = array('category' => $request->category, 'location' => $request->location);
当我将其发送到这样的视图时:
return view('index', compact('currentSelection'));
我收到此消息:
未定义的变量currentSelection
如何使用compact()正确发送数组?
答案 0 :(得分:0)
试试这个:
return view('index')->with(compact('currentSelection'));
或
return view('index', array('currentSelection' => $currentSelection)); // the second param is an array, you can pass multiple elements in this array
您可以在视图刀片视图中访问其值,如:
{{ $currentSelection }}
注意:在您的情况下$currentSelection
是array
,因此您必须使用foreach()
来获取其所有元素
答案 1 :(得分:0)
试
return view('index', compact($currentSelection));
根据艾森海姆的评论:
编辑为
return view('index', compact('varname', $currentSelection));
答案 2 :(得分:0)
请试试这个:
return view('index', compact(['currentSelection']));
答案 3 :(得分:0)
你可以试试这个:
return view('index', [
'currentSelection' => $currentSelection,
]);
希望这对你有用!