Courses

Laravel Travel Agency API From Scratch

Time to build our second API endpoint.


Task Description From Client

A public (no auth) endpoint to get a list of paginated tours by the travel slug (e.g. all the tours of the travel foo-bar).

Users can filter (search) the results by priceFrom, priceTo, dateFrom (from that startingDate) and dateTo (until that startingDate). User can sort the list by price asc and desc. They will always be sorted, after every additional user-provided filter, by startingDate asc.


Skeleton: Controller/Resource/Route

In this lesson, let's build the endpoint itself with automated tests. In the next lesson, we will sort and filter data.

So, similarly to the Travel list lesson, a new Controller:

php artisan make:controller Api/V1/TourController

Also, let's make an API Resource right away:

php artisan make:resource TourResource

Finally, the Route. The client specified that Travel slug should be the parameter. So, we're aiming for the endpoint like /api/v1/travels/[travels.slug]/tours.

So, we write this:

routes/api.php:

use App\Http\Controllers\Api\V1\TourController;
 
Route::get('travels/{travel:slug}/tours', [TourController::class, 'index']);

So, we use Route Model Binding here, specifying the field slug to search for. If we don't specify that, the record will be searched by the travels.id field.

Alternatively, you may specify...

This lesson is only for Premium Members.
Want to access all lessons of this course?

You also get:

  • 68 courses (1188 lessons, 43 h 18 min total)
  • Premium tutorials
  • Access to repositories
  • Private Discord