kdotsmith's avatar

Laravel: Obtain ID of post to check if it already exists

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.

0 likes
6 replies
jlrdw's avatar

I'd start with seeing some past answers on how folks have accomplished this (hotel booking): Copy and paste into Google:

site:laracasts.com hotel booking reservation

Change search as needed. Explanation, a Google search is more powerful than site search here.

kdotsmith's avatar

Qiute a lot to search, im pretty much done with my project however its just this particular thing i need doing

jlrdw's avatar

Seems you need to search by hotel, date, and person who did the booking.

You stated:

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.

But a hotel has many rooms, so date and hotel will only tell you somebody did book a room on a certain date.

But if it's a 200 room hotel, you could have 200 bookings for a date.

Sorry if I misunderstand something.

Snapey's avatar
    $post = Order::where('checkIn', '=', $checkIn)
    ->where ('checkOut', '=', $checkOut)
    //->where('id', '=', $id)
    -first();

get the first entry not a collection. then $post wll be that Order

Snapey's avatar

but....

why would the id not be that of the existing record? Your code does not make any logical sense

kdotsmith's avatar

oh cause the id of the hotel gets added to the cart as shown in the function above. i havent really displayed all the code which is probably why its slightly conusing

Please or to participate in this conversation.