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