本文介绍了Laravel:未定义变量:RoomTypes(视图:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的下拉列表,它将显示数据库中的数据列表.但是我不确定如何使用我的控制器显示它们.我做了这样的事情:

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

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

然后在我看来:

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

如果有人可以帮助我找到一种使用功能在我的页面上显示房间类型的方法,或者可能将该方法实现到将对您有所帮助的getcheckout函数中.

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.

推荐答案

我会尝试以下操作:

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

    $RoomTypes = Room::all();
    $userInputRoomTypes = $request->input('RoomTypes'); // Renamed var to avoid overwriting $RoomTypes

    // ...

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

最后,这可能在您的HTML中起作用.不太确定要通过传递回请求数据来实现什么目的,但是如果要选择一个选项:

Finally, this might work inside your HTML. Not really sure what you are trying to achieve by passing the request data back, but if you want to select an option:

<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}}" {{ !old('RoomType', request('RoomType')) === $RoomType ?: 'selected' }}>{{$RoomType}}</option>
    @endforeach
</select>

这篇关于Laravel:未定义变量:RoomTypes(视图:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:14