Enjoyed This Tutorial?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses
Laravel Coding with AI Agents: Cursor, Claude Code, Codex
How to Build Laravel 12 API From Scratch
Claude Code for Laravel Projects: Crash Course
Comments & Discussion
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!');
}
I think my recent post may help you here: https://laraveldaily.com/post/laravel-custom-validation-rule-with-date-period-and-multiple-fields
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
roomstable with following columns:I need to insert the
room_idintobookingstable like this:I have showed every rooms in the
indexpage: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! 🙏🙏
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 :)
I have stored the following row in my
bookingstableHow can I check those
check_inandcheck_outdates 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! 🙏🙏Sorry, I don't really understand your question: you want me to explain how to do
Booking::where('check_in', $someVariable)?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?
This package is really old and not updated for latest PHP versions, unfortunately we don't have plans to update it, at the moment.