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

axtg's avatar
Level 5

Fully rely on API?

Hi,

I've been wondering what would be the best way to go about splitting certain logic. I started with traditional controllers using Eloquent to retrieve and return data directly into the view, e.g.:

$checkins = Checkin::where('user_id', Auth::user()->id)
                            ->with(['location', 'status'])
                            ->paginate(25);

return view('checkin.index', compact('checkins'));

Now with all the fuzz and focus on having a full API layer, I am wondering whether I should move all data interactions to APIs and only return JSON. That would update above code to _(and move the 'query logic' to the API controller)_:

$client = new \GuzzleHttp\Client([
   'base_uri' => config('app.api'),
 ]);
            
 try 
 {
    $response = $client->request('GET', 'api/v1/checkins');
    $body = $response->getBody()->getContents();
    $checkins = json_decode((string) $body, true);
 }
catch (\GuzzleHttp\Exception\ClientException $exception) 
{
  abort($exception);
}

return view('checkin.index', compact('checkins'));

This allows me to fully separate web and API and have all data logic in one place (API). But on the other hand it feels like making things relatively complex/ slow as I believe it is faster to directly query a data model via Eloquent than it is to do that via an API.

Or obviously there could be a hybrid way, though if I decide to split API from Web, that would mean isolating my Models to a separate repository for both API and Web to refer to.

What would you recommend?

  1. Stay to Eloquent
  2. Move everything to APIs
  3. Use a hybrid model

Thanks.

0 likes
3 replies
Borisu's avatar

In my humble opinion it would be a disaster... Think of the overhead created by the HTTP requests to your own application. Each time you need even basic models you would go out over the wire and ask for it.

Depending on your application you can just check if the request is expecting json and return the model directly. Laravel will automatically serialize it as json for you. If you anticipate to grow to a large app you might consider implementing a full API layer. This is rarely the case, so just do the minimal amount of work. I have personally used this method in production code and it works fine for quite large installations.

1 like
axtg's avatar
Level 5

Hi Borisu,

Appreciate your line of thought, thanks. I think you confirm what is already in the back of my mind.

I'll stick with APIs for anything VueJS, but shouldn't get myself made crazy by any buzz out there. Because indeed, going over a line for everything makes no sense.

Thanks.

shez1983's avatar

there is two reasons for wanting to change it into API -

  1. for learning purposes (think microservices)
  2. if you want to ever extend and create apps/expose your internals via APIs

APIs might create overhead thats true, but when you add varnish, laravel cache etc it wont be that much

Please or to participate in this conversation.