呼叫刀片视图上的未定义变量

时间:2018-08-15 14:40:32

标签: laravel

当我在控制器中的函数 RoomCheck 中调用变量时,我有一个控制器和一个Bootstrap模式:

$availableRoomTypes

在刀片视图中,显示此错误:

  

未定义变量:availableRoomTypes(视图:/var/www/html/viure/Modules/Comprof/Resources/views/public/room-check-modal.blade.php)**

这是我的控制器中的 RoomCheck 功能:

 public function RoomCheck($start, $end)
        {
            if (!$request->wantsJson()) {

                return $request;
            }
            $this->validate($request, [
                'checkIn' => 'required',
                'checkOut' => 'required',
            ]);

            $start = Carbon::parse($request->checkIn);
            $end = Carbon::parse($request->checkOut);

            $start_at = $start->format('Y-m-d');
            $end_at = $end->subDays(1)->format('Y-m-d');

            $roomsTypes = RoomType::all(); 

             $availableRoomTypes  = $roomsTypes->map(function ($type) use ($start, $end) {
                $unavailabilities = Unavailability::whereBetween('date', [$start, $end])
                ->where('room_type_id', $type->id)->get();
                $group = $unavailabilities->groupBy(function ($item) {
                    return $item->room_type_id . '_' . $item->reservation_id;
                });

                $totalReservation = $group->map(function ($query) {
                    return $query->first();
                });

                $type->availableroom = $type->room_count - $totalReservation->sum('quantity');

                return $type;
            });

            return response()->json([
                'date' => $request->checkIn,
                'availableRoomTypes' => $availableRoomTypes,
            ]);
        }

这是我的模态刀片视图:

<div class="modal-body">
      <table class="table">
        <thead>
          <tr>
            <th>Room Type
            @foreach($roomTypes as $roomType)
            <option value="{{ $roomType->id }}" @if (old('room_type_id') == $roomType->id) selected @endif>
              {{ $roomType->name }}
            </option>
          @endforeach
          </th>
            <th width="130">Available Room</th>
            @foreach($availableRoomTypes as $availableRoomTypess)
            <option value="{{ $availableRoomTypes }}" @if (old('availableRoomTypes') == $availableRoomTypes) selected @endif>
              {{ $availableRoomTypes }}
            </option>
          @endforeach
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>

但是,当我从同一控制器中的Store函数中调用 $ roomTypes 时,没有错误,但是当我调用 $ availableRoomTypes 时,出现了上述错误。怎么了?

操作表单位于 roombook.blade.php 文件中,我的资金自举程序包含在Roombook文件中,其中包含到路线存储的操作表单

我怎么了?请帮我。谢谢!

1 个答案:

答案 0 :(得分:2)

您的控制器通常会在调用视图时返回in变量,例如:

return view('roombook', ['date' => $request->checkIn,
            'availableRoomTypes' => $availableRoomTypes,]);