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

rajoyish's avatar

Book rooms using Laravel

Dear artisans, how can I implement a simple room booking system without using any fancy tools such as Alpine.js, Livewire or any frontend frameworks but just using vanilla Laravel?

I have rooms table with following columns:

$table->id();
$table->string('name');
$table->integer('price');

I need to insert the room_id into bookings table like this:

$table->id();
$table->unsignedBigInteger('room_id');
$table->foreign('room_id')->references('id')->on('rooms')->cascadeOnDelete();
$table->date('check_in');
$table->date('check_out');
$table->integer('amount');
$table->boolean('status')->default(false);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();

I have showed every rooms in the index page:

<a href="{{ route('rooms.show', $room) }}">Book Now</a>

From there user can view the room and book using a button.

I know this needs entire CRUD operations and time for a course. Your help is highly appreciated. Thank you! 🙏🙏

0 likes
5 replies
Sinnbeck's avatar

What exact part are you struggling with?

1 like
rajoyish's avatar

@Sinnbeck

I have written store() method in BookingController.php. How can refactor this? Would you please help me. Thank you!


public function store(Request $request)
    {
        $request->validate([
            'check_in' => ['required', 'date'],
            'check_out' => ['required', 'date'],
            'amount' => ['numeric'],
        ]);

        if (Booking::where('room_id', $request->room_id)->exists()) {
            return to_route('rooms')->with('error', 'Sorry! The room is already booked!');
        }

//        Days based on check in & check out
        $check_in = Carbon::parse($request->check_in);
        $check_out = Carbon::parse($request->check_out);
        $days = $check_out->diffInDays($check_in);

//        Rounding up to 1 if user chooses the same date
        $days = $days == 0 ? 1 : $days;

//      Total price
        $room = Room::find($request->room_id);
        $price = $room->price;
        $total = $days * $price;

//        Total amount with discount
        $discount_id = Auth::user()->discount_id;
        $discount = Discount::find($discount_id);
        $discount_amount = $discount->percentage;
        $amount = $total - ($total * ($discount_amount / 100));

        Booking::create([
            'room_id' => $request->room_id,
            'check_in' => $check_in,
            'check_out' => $check_out,
            'amount' => $amount,
            'status' => false,
            'user_id' => \Auth::id(),
        ]);

        return to_route('user.dashboard')->with('success', 'Booked! Please pay the invoice to get confirmed.');
    }

Sinnbeck's avatar

@rajoyish Ok so the user needs to be logged in to book it? The code as such is fine. A few ideas

  1. Use a form request to not validate in the controller
  2. Move the booking to a helper class. It could be on the booking model or a dedicated class like an Action class
1 like
Sinnbeck's avatar

This query looks a bit too simple. If any bookings exists on the room at all, it fails? I assume you want to check the checkin period to find any overlaps

        if (Booking::where('room_id', $request->room_id)->exists()) {
            return to_route('rooms')->with('error', 'Sorry! The room is already booked!');
        }
rajoyish's avatar

I'm working on it but it seems messy. I will post store method of BookingController after finishing some things up.

Please or to participate in this conversation.