Search code examples
laravellaravel-5eloquentbootstrap-modallaravel-blade

Undefined variable in boostrap modal - Laravel


I have a $room variable inside bootstrap modal and it is causing the problem.

Here is the function in the reservationController:

public function create()
{
    $room = Room::all();
    return view('/allReservationChild', compact('room'));
}

The blade name is allReservationChild.blade.php and this is the part of the modal:

<div class="modal fade" id="addres" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Delete Room</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form style="margin: 50px; text-transform: capitalize " action="/addReservation" method="POST" class="needs-validation" novalidate >
                @csrf

                <div class="modal-body">
                    <div class="form-group">
                        <label for="exampleFormControlSelect1">Select the Room you want to reserve</label>
                        <select class="form-control" id="exampleFormControlSelect1" name="room_id" required>
                            <option value=""></option>
                            @foreach($room as $items)
                                <option value="{{$items->id}}">{{$items->id}}</option>
                            @endforeach

                        </select>
                    </div>


                    <div class="mb-3">
                        <label for="exampleFormControlTextarea1" class="form-label">Objectif :</label>
                        <select class="form-control" id="exampleFormControlTextarea1" name="objectif" required>
                            <option value=""></option>
                            <option value="Booking room for a replacement session ">Booking room for a replacement session </option>
                            <option value="Booking room for a random session">Booking room for a random session</option>
                            <option value="Booking room for an event">Booking room for an event</option>
                            <option value="Booking room for an exam">Booking room for an exam</option>
                            <option value="Booking room for discussion of the graduation thesis">Booking room for discussion of the graduation thesis</option>
                            <option value="other">other</option>
                        </select>
                    </div>


                    <div class="form-group">
                        <label class="control-label">Reservation date</label></br>
                        <input class="form-control" type="date" id="Date_beginning" name="date" required>
                    </div>

                    <div class="form-group">
                        <label for="time">Select time period</label>
                        <select class="form-control" id="time" name="time" required>
                            <option value=""></option>
                            <option value="8:30-10:30">8.30-10.30</option>
                            <option value="10:30-12:30">10.30-12.30</option>
                            <option value="13:30-15:30">13.30-15.30</option>
                        </select>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-outline-danger" name="supp" >Add Reservation</button>
                </div>
            </form>

        </div>
    </div>
</div>

And the route is:

Route::get('/addReservation', [\App\Http\Controllers\reservationController::class,'create']);

I don't know why I can not define the $room variable. I think it is because of the modal or something.

And I'm sure the modal is right because I used the same modal in another blade and it works perfectly so I think the problem is in the controller or the web, I am not sure.

EDIT: I have a reservation modal and table, when the user clicks on Add new Reservation the pop up should show up, and then he will choose one of the existed rooms


Solution

  • I am not much confident about path to view. You can try out this is case

    public function create()
    {
        $room = Room::all();
        return view('/allReservationChild', ["room"=>$room] );
    }
    
    

    I hope it works for you

    Example it's works on live project for me

        public function index()
        {
    
            $educationList = Cache::rememberForever('educationList', function () {
    
                return EducationMaster::where('education_status', 1)->get();
            });
    
            return view('Admin.pages.Masters.EducationMaster.index', ['educationList' => $educationList]);
        }
    
    

    On my view

    <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
                    aria-labelledby="staticBackdropLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="staticBackdropLabel">
                                    {{ __('common.add new') }}-{{ __('masters.City') }}</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"
                                    aria-label="Close"></button>
                            </div>
                            <form action="{{ route('admin.city.store') }}" method="post">
                                @csrf
                                <div class="modal-body">
                                    <div class="container">
                                        <div class="row">
                                            <div class="col">
                                                <label class="form-label">{{ __('masters.City') }}</label>
                                                <input type="text" name="city_name" class="form-control" required>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col">
                                                <div class="mb-3">
                                                    <div class="form-label">{{ __('masters.State') }}</div>
                                                    <select class="form-select" name="state_id" required>
                                                        <option value="">Choose The State</option>
                                                        @foreach ($stateList as $state)
                                                            <option value="{{ $state->id }}">{{ $state->state_name }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
    
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-square-x"
                                            width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"
                                            fill="none" stroke-linecap="round" stroke-linejoin="round">
                                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                            <rect x="4" y="4" width="16" height="16" rx="2"></rect>
                                            <path d="M10 10l4 4m0 -4l-4 4"></path>
                                        </svg>
                                        {{ __('common.cancel') }}</button>
    
                                    <button type="submit" class="btn btn-primary " x-on:click="addNewRasi()">
                                        <svg xmlns="http://www.w3.org/2000/svg"
                                            class="icon icon-tabler icon-tabler-device-floppy" width="24" height="24"
                                            viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                                            stroke-linecap="round" stroke-linejoin="round">
                                            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                            <path d="M6 4h10l4 4v10a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2">
                                            </path>
                                            <circle cx="12" cy="14" r="2"></circle>
                                            <polyline points="14 4 14 8 8 8 8 4"></polyline>
                                        </svg>
                                        {{ __('common.save') }}</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
    
    
    ``