Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kdotsmith's avatar

Laravel: Check if record exists in database

So i currently have validation on my system that will check if the two dates have been entered in before. If they have the user cannot proceed to the checkout and ofc if they have different dates they can proceed. However the problem i am getting, is that this validation works for any hotel and should only be specific to one hotel that they are booking.

For example, if hotel one is booked for 18/04/2019 and i want to book hotel two for the same days, the validation will not let me check out as the dates are the same. This is incorrect and it should let me through as its a different hotel even though the dates are the same.

I want to now proceed and check if the same hotel and same dates have been chosen and if they are the same then we cannot proceed. The function below is used for my validation:

public function getCheckout(Request $request)
{
    if (!Session::has('cart')) {
        return view('shop.shopping-cart');
    }
    //$post = Post::find($id);
    $oldCart = Session::get('cart');
    $cart = new Cart($oldCart);
    $total = $cart->totalPrice;
    $checkIn = $request->input('checkIn');
    $checkOut = $request->input('checkOut');

    $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');
 } else {
    return view('posts.checkout', ['total' => $total, 'checkIn' => $checkIn, 'checkOut' => $checkOut]);
    
}

}

Now the problem is that i also have no passing of the hotels $id. The function below originally adds my $id to the cart, how do i exactly add the $id into the 'getCheckout' function and validate it ?

  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');
    }

If anyone could help that will be great. I just simply need to pass the hotels ID into my check and check if the hotel, check in and check out dates all match

0 likes
1 reply
Snapey's avatar

How can you take a booking if you don't know which hotel it is for?

Why is everything called Post?

Please or to participate in this conversation.