Laravel: Undefined variable: RoomTypes (View:

懵懂的女人 提交于 2021-02-17 06:23:06

问题


So i have a simple drop down list that will display the list of data from my database. However im not sure how to display them using my controller. I did something like so:

ShoppingCart.blade.php

public function getCheckout(Request $request)
{
    if (!Session::has('cart')) {
        return view('shop.shopping-cart');
    }

    $RoomTypes = Room::all(); // RoomTypes are defined here
    $oldCart = Session::get('cart');
    $cart = new Cart($oldCart);
    $total = $cart->totalPrice;
    $checkIn = $request->input('checkIn');
    $checkOut = $request->input('checkOut');
    $RoomTypes = $request->input('RoomTypes');
    $datetime1 = new DateTime($checkIn);
    $datetime2 = new DateTime($checkOut);
    $interval = $datetime1->diff($datetime2);
    $days = $interval->format('%a'); // now do whatever you like with $days
    $total = $days * $cart->totalPrice;

    $post = Order::where('checkIn', '=', $checkIn)
        ->where('checkOut', '=', $checkOut)
        ->get();

    if (count($post) > 1) {
        return redirect()->route('posts.shopping-cart')->with('Sorry this date has been taken');
    }

    return view('posts.checkout', [
        'total'     => $total,
        'checkIn'   => $checkIn,
        'checkOut'  => $checkOut,
        'RoomTypes' => $RoomTypes,
    ]);
}

Then in my view:

<select name="RoomType" id="RoomType" class="form-control input-lg dynamic" data-dependent="state">
    <option value="">Room type</option>

    @foreach($RoomTypes as $RoomType)
        <option value="{{$RoomType}}">{{$RoomType}}</option>
    @endforeach
</select>

If anyone could help me figure out a way of displaying the room types on my page using a function or potentially implementing the method into the getcheckout function that will help.

来源:https://stackoverflow.com/questions/61305886/laravel-undefined-variable-roomtypes-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!