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

natew's avatar
Level 1

Laravel API - Best practices for consuming?

I've currently got a Vue frontend powered by a Laravel API.

I have a user profile section which needs to query 5 different tables within the database to get the required data for several dynamic select boxes and inputs on my profile page on the frontend.

I have two approaches:

  1. Make 5 separate Axios calls to the Laravel API to get the required data. This approach de-couples everything but it comes with the burden of 5 separate requests, each of which need to run through a Middleware for Authentication, Routing to appropriate schema, Setting configs, etc.

  2. Make 1 Axios call to the Laravel API to get all the data required at once for all the different table data. This reduces it to a single request, but the data returned is all kind of mushed together, which makes it tightly coupled.

I cannot do a single query to join all the tables. As the information they contain is not linked.

What is normally done in this scenario?

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

For me, a single request is preferable in this scenario, especially where it is a single purpose endpoint. I wouldn't be worried about tight coupling; you can structure response data however you would like. If you make a class that implements Responsable interface which has responsibility for building the responsse data, you can return this directly from the Controller:

class Profile implements Responsable
{
	protected function getUserDetail()
    {
        // query for data
        // return array structure of data you want to expose.
    }

    public function toResponse($request)
   {
        return response([
			'user_detail' => $this->getUserDetail(),
            // ...
        ]);
   }
}
// Controller action
public function show()
{
		return new Profile();
}

If the class needs some params - just create a constructor to accept them.

1 like
natew's avatar
Level 1

@tykus Thanks that looks like a solid solution to go for. Will give it a try.

xdimension's avatar

This case is a really good example of what GraphQL solves. But, if it's too overkilled for your project, tykus' suggestion is a good one.

Please or to participate in this conversation.