Laravel Room Booking: with QuickAdminPanel and Stripe

avatar

Dear Povilas,

I have imlemented the lessons learned from your course "[Mini-Course] Laravel Breeze with User Role Areas". I am building a project for a client. How can I implement a simple room booking system without using any fancy tools such as Alpine.js, Livewire, Packages 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! 🙏🙏

avatar

Ok, so which part exactly you need to help with? It's not something I can answer in a comment, and I can't create a full new course for you for all booking mechanism, I don't have that much free time available at the moment :)

avatar

I have stored the following row in my bookings table

{
  "id": "1",
   "check_in": "2022-12-13",
  "check_out": "2022-12-20",
  "user_id": 1
}

How can I check those check_in and check_out dates that do not exists in the columns and store those dates using Eloquent or Query Builder? Your help is much appreciated, thank you so much! 🙏🙏

avatar

Sorry, I don't really understand your question: you want me to explain how to do Booking::where('check_in', $someVariable)?

avatar

hello Mr. Povilas Korop, I've tried to download this package but the issue was that my PHP version = 8.1.10. And it returned a big number of lists or errors.. I've tried other packages but still didn't work any of them (same issue..). How can I resolve this issue? do you have a general approach for this and other packages with same kind of issue?

avatar

This package is really old and not updated for latest PHP versions, unfortunately we don't have plans to update it, at the moment.

avatar

I want to store a booking in different dates that are not stored in the table already. How I disallow a user to make booking with same room in the same check-in and check out dates that is already booked? In other words, a room can be booked only once with different dates. I don't want to overlaps booking with same room with dates.

Here' what I have done so far :

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

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

        if (Auth::user()->is_verified === false) {
            return back()->with('error', 'Sorry! Your account is not verified, please call us to do so.');
        }

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

        // abort if user check-ins in the past
        if ($check_in->lessThan(today())) {
            return back()->with('error', 'Sorry! you cannot check-in the past.');
        }

        $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_amount = \Auth::user()->discount->percentage;
        $amount = $total - ($total * ($discount_amount / 100));

        $booking = Booking::create([
            'check_in' => $check_in,
            'check_out' => $check_out,
            'amount' => $amount,
            'user_id' => \Auth::id(),
        ]);

        // booking_id
        Auth::user()->bookings();

        // room_id
        $booking->rooms()->attach($request->room_id, ['status' => (bool) false]);

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

I have done a mess here, I don't like this code at all and don't know how to write the logic:


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

Hi, This is one of my favourite projects. Are there any plans to upgrade to laravel 10 or 11? Thanks

avatar

Hi, we might upgrade this, but there is no ETA on when. We are slowly working over all the courses we had (prioritizing text ones for now)