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

ignisrzeus's avatar

Laravel API Query String Route

Hi! I'm fairly new to building APIs using Laravel, and I was wondering why when I use query strings in postman, the data returned will always be from my index method.

Meaning, when I go to (.../api/this_table?status=Typed), instead of getting all columns of "this_table" where their status is only "Typed", i instead receive all of the columns of "this_table".

I was wondering if there is anything I need to do in my routes to enable Query Strings in Laravel, because I think it may be the problem. I did not assign any routes for Query Strings, and I've been searching how to do it but I unfortunately can't see the right answer.

Thanks, Laracasts!

0 likes
6 replies
tykus's avatar

You can access the query string parameters from the Request object, not at Route level:

// in the controller action:
$status = $request->get('status');

// do stuff with $status

You can use the request() helper method if you prefer:

$status = request('status');
ignisrzeus's avatar

@tykus Hi! I have to admit, i did not clearly understand your answer (simply because I'm a noob in API). Can you please explain to me where and why I'm supposed to add those lines?

tykus's avatar

in the controller action:

Say you have a GET route defined, which maps to a particular controller action:

Route::get('this_table', 'ThisTableController@index');

Then in the ThisTableController, your index method might look like:

public function index(Request $request)
{
    $status = $request->get('status');

    // Get a Builder instance
    $query = ThisTable::query();

    if ($status) {
        // . Conditionally add a WHERE
        $results->where('status', $status);
    }

    // Finish the query
    $results = $query->get();
    
    // return the response
    return response()->json($results);
}
ignisrzeus's avatar

I think there may be a misunderstanding in my question. (.../api/this_table) returns ALL columns from this_table (As expected, because my index method was tied to this route).

(.../api/this_table) returns me:

[{"id":1,"donation_id":"NVBSP20190808998","status":"Processed","created_at":"2019-06-27 05:59:10","updated_at":"2019-06-28 04:00:18"},{"id":2,"donation_id":"NVBSP20190808999","status":"Typed","created_at":"2019-06-27 05:59:10","updated_at":"2019-06-28 04:00:18"}]

This what I was expecting to get from (.../api/this_table?status=Typed): {"id":2,"donation_id":"NVBSP20190808999","status":"Typed","created_at":"2019-06-27 05:59:10","updated_at":"2019-06-28 04:00:18"}

But I instead got the same result from (.../api/this_table). I hope this clears up my problem. Thanks in advance.

ignisrzeus's avatar

Hi @tykus ! Thanks for the fast response. It's now working, thanks! However, does this mean that I have to identify each column for my table and add an if statement? Let's say I want donation_id instead of status, will I then manually add an if statement just for the donation_id column?

Thanks!

Please or to participate in this conversation.