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

Gabriel Rogerio Severo's avatar

How can I use my api made in Laravel?

Everything works fine when I use with ajax, insomnia or postman, but when I try to query through a controller, the page just gets infinitely loaded, could anyone help me?

My api route:

Route::get('/getAllUsers', [ApiUsersController::class, 'getAllUsers'])->name('getAllUsers');

My api controller returning all users:

public function getAllUsers() {

    $users = User::all();

    return response()->json($users);
}

My web route:

Route::get('/apiTest', [TestController::class, 'apiTest'])->name('apiTest');

My request in controller:

public function apiTest() {

    $response = Http::acceptJson()->withToken('Bearer token')
    ->get('localhost/api/getAllUsers');

    return response()->json($response);
}
0 likes
10 replies
Tippin's avatar
Tippin
Best Answer
Level 13

Why delete the thread instead of editing it? I was just commenting on your deleted post...

Also, you don't consume your own API by using the HTTP client from a web controller to access your API controllers. The reason you are being timed out is probably because you are serving locally, thus a single process, and while one request is trying to be completed, you are asking for another process to hit another http endpoint before the current request on your single process from your local webserver can complete the first one.

Are you trying to write an actual feature test? Otherwise, why are you doing what you are doing?

Gabriel Rogerio Severo's avatar

@Tippin Sorry, I'm new to this platform. Can you tell me how to consume my api? I have no experience with this but I need to do this

Tippin's avatar

@Gabriel Rogerio Severo Well, usually "consuming" your own API means you have a client/UI side or a single page app that uses your API's json responses to display data. If you are just using view/html responses, then your web controller should handle querying the database and sending the data to the view, not querying an internal API http call to get the data.

If you had a need for a list of users (which you should also paginate, not use all), well perhaps a web controller will query to the database to get such records, but you may also have a job or command class that needs the same query, so you would query there, or put the query in something like a service class that will allow you to reuse the same query in multiple places within your app.

1 like
tykus's avatar

@Gabriel Rogerio Severo but you are trying to comsume it using a web route? Do you actually have a SPA to consume the API?

Gabriel Rogerio Severo's avatar

@tykus I don't have it, I basically just created the routes in the routes/api.php file and I'm trying to consult with http client and then pass it to a view.

tykus's avatar

@Gabriel Rogerio Severo seems to me that you don't need an API in that case? What would initiate a Request to the apiTest endpoint?

Tippin's avatar

@Gabriel Rogerio Severo I agree with @tykus you do not need an API in this case. An API is meant to be directly used via HTTP calls from a client, whether that be your own SPA or a mobile app, or 3rd party entities if your API was public. But if you are simply trying to pass data to your view, then your controller registered in your web routes will be the one to query your database and pass that data to the view.

Please or to participate in this conversation.