Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abkrim's avatar
Level 13

Mistake with get with Query params

Hello

I'm running into a problem that I can't see. I am doing a test of an endpoint that works perfectly. I use Laravel 10 (update yesterday) and Spatie/QueryBuilder.

My Controller CommandCenterApiController

public function index(CommandCenterFilterRequest $request)
{
   ray($request->all())->die(); // Show empty array
   // More code
}

Well, if I run the query {{local}}/api/v2/command-centers?filter[town_hall_id]=2 in postman I get the models that have town_hall_id = 2.

But if I do it in the test, it returns all of them.

When doing debugging, I see $request->all() returns an empty array [] so the JSON response is the return of all the models and not the ones that have 2 as town_hall_id.

Test

function api_call_command_center_with_filter_by_townhall()
{
    Sanctum::actingAs(User::factory()->create());

    $manufacturer = Manufacturer::where('name', 'Solar Power')->first();
    $townHall = TownHall::factory()->create();

    $commandCenter = CommandCenter::factory(10)->create([
        'manufacturer_id' => $manufacturer->id,
        'town_hall_id' => $townHall->id,
    ]);

    CommandCenter::factory(10)->create([
        'manufacturer_id' => $manufacturer->id,
    ]);

    $params = [
        "filter[town_hall_id]" => 2,
    ];
    
    $response = $this->getJson(route('api.v2.command-centers.index'), $params);

    ray($response->json());
}
0 likes
0 replies

Please or to participate in this conversation.