JSON:API provides many options for filtering, sorting, and including extra data into the requested data using query parameters. Let’s take an example of an endpoint where we want to get a list of Projects, which has the following data model: Project: attributes: id: string name: string description: text status: string (Enum: planning, in-progress, in-testing, done) active: boolean relationships: owner: BelongsTo (User) client: BelongsTo (Client)

{ return QueryBuilder::for( subject: $builder, )->allowedIncludes( includes: ['owner', 'client'], )->allowedFilters( filters: ['status', 'active'], )->where( 'user_id', $user, )->getEloquentBuilder(); }}

it('can fetch the projects client', function (): void { actingAs(User::factory()->create())->getJson( uri: action(IndexController::class, [ 'include' => 'client', ]), )->assertStatus( status: Http::OK->value, )->assertJson(fn (AssertableJson $json) => $json ->first(fn (AssertableJson $json) => $json ->has('relationships.client') ->etc()

This is a simple guide to test JSON:API endpoints in Laravel using pestPHP.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners