Skip to main content

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? (36 h 00 min)

You also get:

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

Already a member? Login here

Ali Al Qahtani avatar

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.

Ali Al Qahtani avatar

I think we need used query in (Show) method

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

Povilas Korop avatar

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

Ali Al Qahtani avatar

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);

Povilas Korop avatar

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

Povilas Korop avatar

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 :)

Ali Al Qahtani avatar

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

WILLY CHANDRA NEGARA avatar
WILLY CHANDRA NEGARA

Any fix for this ?

Emre Dikmen avatar

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);
}
});
}
Ngozi Stephen Onyemauche avatar
Ngozi Stephen Onyemauche

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

Povilas Korop avatar

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.

Luis Antonio Parrado avatar
Luis Antonio Parrado

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!!

Povilas Korop avatar

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

Njuguna Mwangi avatar

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

Modestas avatar

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

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.