Home > Mobile >  Undefined variable in boostrap modal - Laravel
Undefined variable in boostrap modal - Laravel

Time:05-08

I have a $room variable inside a 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  id="addres" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div  role="document">
        <div >
            <div >
                <h5  id="exampleModalLongTitle">Delete Room</h5>
                <button type="button"  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"  novalidate >
                @csrf

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

                        </select>
                    </div>


                    <div >
                        <label for="exampleFormControlTextarea1" >Objectif :</label>
                        <select  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 >
                        <label >Reservation date</label></br>
                        <input  type="date" id="Date_beginning" name="date" required>
                    </div>

                    <div >
                        <label for="time">Select time period</label>
                        <select  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 >
                    <button type="button"  data-dismiss="modal">Close</button>
                    <button type="submit"  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.

CodePudding user response:

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  id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
                aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div >
                    <div >
                        <div >
                            <h5  id="staticBackdropLabel">
                                {{ __('common.add new') }}-{{ __('masters.City') }}</h5>
                            <button type="button"  data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <form action="{{ route('admin.city.store') }}" method="post">
                            @csrf
                            <div >
                                <div >
                                    <div >
                                        <div >
                                            <label >{{ __('masters.City') }}</label>
                                            <input type="text" name="city_name"  required>
                                        </div>
                                    </div>
                                    <div >
                                        <div >
                                            <div >
                                                <div >{{ __('masters.State') }}</div>
                                                <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 >
                                <button type="button"  data-bs-dismiss="modal">
                                    <svg xmlns="http://www.w3.org/2000/svg" 
                                        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"  x-on:click="addNewRasi()">
                                    <svg xmlns="http://www.w3.org/2000/svg"
                                         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>


``

CodePudding user response:

Did you include your modal in Blade file ??

  • Related