错误是:
未定义变量:room_state(查看: /var/app/current/resources/views/pages/room.blade.php)
在我的控制器中,我有:
$room_state = 1;
$rooms = DB::table('room')->select('*')->where('room_id', '=', $room_id)->get();
$doors = DB::table('door')->select('*')->where('room_id', '=', $room_id)->get();
foreach ($rooms as $room) {
if (empty(session()->get('user.rooms')) || ! in_array($room->room_id, session()->get('user.rooms')) ) {
session()->push('user.rooms', $room->room_id);
$room_state = 0;
}
else {
$room_state = 1;
}
}
return view('pages/room', ['rooms' => $rooms], ['doors' => $doors], ['room_state' => $room_state] );
我认为,我有:
<p>Room State: {{ $room_state }}</p>
在添加房间状态以返回之前,我的视图正在工作,我在做什么错了?
答案 0 :(得分:3)
您要传递多个参数,而不是将具有所有键值对的单个参数传递给您的view()
调用。
执行此操作:
return view('pages/room', ['rooms' => $rooms, 'doors' => $doors, 'room_state' => $room_state]);
或者,只需使用compact()
,因为变量名与键名相同:
return view('pages/room', compact('rooms', 'doors', 'room_state'));
答案 1 :(得分:0)
尝试仅在一个数组中返回值
return view('pages/room', ['rooms' => $rooms,'doors' => $doors, 'room_state' => $room_state]);
或者您使用紧凑型
return view('pages/room', compact('rooms', 'doors', 'room_state));