Bian
Bian

Reputation: 93

Undefined variable on call blade view

I have a controller and a Bootstrap modal, when I call the variable in the function RoomCheck in my controller:

$availableRoomTypes

In the blade view, this error is displayed:

Undefined variable: availableRoomTypes (View: /var/www/html/viure/Modules/Comprof/Resources/views/public/room-check-modal.blade.php)**

This is my RoomCheck function from my controller:

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

This is my modal blade view:

<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>

But when I call $roomTypes from the Store function in the same controller there is no error, but when I calling the $availableRoomTypes I get the error like above. What is wrong?

The action form is in the roombook.blade.php file, my capital bootstrap includes in the roombook file with the action form to the route store

What is my wrong? Please help me. Thanks!

Upvotes: 1

Views: 257

Answers (1)

Luke G.
Luke G.

Reputation: 587

Your controller typically will return the variable in when calling the view, such as:

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

Upvotes: 2

Related Questions