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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners