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 for multiple services

I'm designing a Laravel API and have the following problem.

The API will be consumed by a Mobile Application, a Web Application (running Vue), as well as third parties using the API to extract data for their own use.

Lets say I have a User table which has 30 different columns in the database, the Mobile Application may only need 4/30 of these columns, the Web Application may need some different arbitrary amount, for example 8/30 columns and the third parties would need 30/30.

I feel it's a waste of resources to return all 30 columns to Mobile Application which is only going to need 4 of them on every request.

How can I satisfy all 3 parties at once, am I forced to return all 30 columns to the Mobile Application?

I have about 100 tables, so this would be spread across them all and lots of not needed data would be pulled from the API all the time.

Is there a way to approach this problem so each application/party only gets what they want? Without creating extra API endpoints for each of them.

0 likes
3 replies
martinbean's avatar

@natew One option would be to let the clients specify the fields they want included in the response. The JSON:API spec has a concept of “sparse fieldsets” where you can specify the fields you want included in the response: https://jsonapi.org/format/#fetching-sparse-fieldsets

However, I’d ask yourself if the development effort to support something like this is worth the effort? If a client makes a request and gets the fields it needs, then they can just discard the ones they don’t care about.

BezhanSalleh's avatar

@natew you coulde setup headers based on the client and select then return what's needed.

frontend i.e:

//mobile app

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  headers: {'X-Device: 'mobile'}
});

//web app

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  headers: {'X-Device': 'web'}
});

backend i.e:

//authentication stuff...

if($request->header('X-Device') == 'mobile') {
        return Post::select('col1','col2','col3')->get();
}

if($request->header('X-Device') == 'web') {
        return Post::select('col1','col2','col3','col4','col5','col6')->get();
}

//thirdparty clients
return Post::get();

hope it helps.

martinbean's avatar

An API should not return different responses based on what’s consuming it. You should get the same response whether you make the request through PHP, cURL, Postman, iOS, Android…

Please or to participate in this conversation.