So im doing a validation check on my records to see if the record already exists. The validation currently compares the dates and see if they have already been entered in and if they have a message appears saying you cannot book these days. Great, however i need to find the ID of that particular hotel that matches them dates too. This is so the user cannot select a hotel which has been booked already on the days they select. Here is the code:
public function getCheckout(Request $request)
{
if (!Session::has('cart')) {
return view('shop.shopping-cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$total = $cart->totalPrice;
$RoomTypes = DB::table('rooms')->select('RoomType')->distinct()->get()->pluck('RoomType');
$checkIn = $request->input('checkIn');
$checkOut = $request->input('checkOut');
$myRoom = $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)
//->where('id', '=', $id)
->get();
if(count($post) > 1) {
return redirect()->route('posts.shopping-cart')->with('Sorry this date has been taken');
} else {
return view('posts.checkout', ['total' => $total, 'checkIn' => $checkIn, 'checkOut' => $checkOut, 'myRoom'
=> $myRoom]);
}
In my getAddtoCart method, you will notice how the cart adds the ID of the hotel into it like so:
public function getAddToCart(Request $request, $id)
{
$post = Post::find($id);
$cart = Session::has('cart') ? Session::get('cart') : null;
if(!$cart)
{
$cart = new Cart($cart);
}
$cart->add($post, $post->id);
Session::put('cart', $cart);
// dd($request->session()->get('cart'));
return redirect()->route('posts.index');
}
So how exactly would i manage to add my ID into the proposed getCheckout function to make sure the hotel ID, Checkin and checkout dates all get validated.