Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Make Bookings and View/Cancel Them

Premium
14 min read

We're finally at the point when we can make bookings, yay.


Goals of This Lesson

  • Booking DB Model/Migration
  • API endpoint & first successful booking
  • Calculating the total price of booking
  • Validation: apartment capacity and availability
  • Viewing and canceling user's bookings

As usual, covered by tests, so by the end of this lesson, we will have these tests passing:


Booking DB Model/Migration

First, the database structure for that...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

AA
Ali Al Qahtani ✓ Link copied!

In (Show) method in (BookingController):

Are you sure can show the result with trashed when we used Model Binding?

Becouse the method return 404, so the test (test_user_can_cancel_their_booking_but_still_view_it) will fail.

AA
Ali Al Qahtani ✓ Link copied!

I think we need used query in (Show) method

$booking = Booking::where('id', $booking)->withTrashed()->first();

PK
Povilas Korop ✓ Link copied!

Not 100% sure actually, but in the repository that test didn't fail. Could you compare to the repository, please?

AA
Ali Al Qahtani ✓ Link copied!

Yes I compare to repository, still (test_user_can_cancel_their_booking_but_still_view_it) fail.

In these lines assert status 200 but in my test return 404, so the test fail

$response = $this->actingAs($user1)->getJson('/api/user/bookings/' . $booking->id); $response->assertStatus(200);

PK
Povilas Korop ✓ Link copied!

Just cloned the last version of the repo, and that test passes for me: ✓ user can cancel their booking but still view it

PK
Povilas Korop ✓ Link copied!

That said, found that another test is failing after my last new lesson, so thanks for reminding me to rerun php artisan test, need to update another lesson :)

AA
Ali Al Qahtani ✓ Link copied!

Thanks for you, This course is the most course I found it for me, It contains more than learning how to build project.

WC
WILLY CHANDRA NEGARA ✓ Link copied!

Any fix for this ?

ED
Emre Dikmen ✓ Link copied!

Yes, I organized the messages in json format like this. app/Exception/Handler.php

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Http\Request;
   public function register()
    {
        $this->renderable(function (NotFoundHttpException $e, Request $request) {
            if ($request->is('api/*')) {
                return response()->json([
                    'message' => $e->getMessage()
                ], 404);
            }
        });
    }
NS
Ngozi Stephen Onyemauche ✓ Link copied!

I ran test for test_user_can_book_apartment_successfully_but_not_twice() and i got this error message. pls how do i solve it. SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: bookings.start_date (Connection: sqlite, SQL: insert into "booking s" ("apartment_id", "user_id", "total_price", "updated_at", "created_at") values (1, 2, 0, 2023-05-29 02:23:05, 2023-05-29 02:23:05)) I am following the course step by step

PK
Povilas Korop ✓ Link copied!

It's hard to debug the error for you, as I didn't encounter that error myself. For some reason, the start_date is empty in your case, how can it be null, maybe you're not passing it? Every booking needs to have a start_date. Or maybe you haven't defined it in the Factory? Please check your code and compare to the original repository.

LA
Luis Antonio Parrado ✓ Link copied!

test_user_can_cancel_their_booking_but_still_view_it fails for me, it doesn't find cancelled (softDeleted) booking. It's strange because in Postman they do work!!

PK
Povilas Korop ✓ Link copied!

@Luis can't really comment or debug without seeing the full code, sorry

NM
Njuguna Mwangi ✓ Link copied!

Question:

Route::prefix('user')->group(function () { Route::resource('bookings', \App\Http\Controllers\User\BookingController::class); });

You instruct that this route be in routes/web.php, Shouldnt it be in routes/api.php

M
Modestas ✓ Link copied!

Thank you for spotting this - indeed it should have been in the API file!