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

cyprous's avatar

API resources or Eloquent models?

Hi,

I have been reading about this concept of API resources and how they are often used to create external API. My question is when should I stop returning Eloquent models and start using API resources?

Is it only for external APIs ?

Can I use Eloquent Models for internal APIs?

If I mainly use blade views for creating visual layer and only sprinkle a little bit of Vue js, do I need to rely on internal API?

I am asking - what is the general rule of thumb and where/what scenarios to use each or both?

Thanks

0 likes
5 replies
Sinnbeck's avatar

Personally I use them for an internal api consumed by react. It makes it easy for me to specify exactly what I need in react app. I also makes it quick to get an overview of what my controllers return.

jlrdw's avatar

An api is an interface to something. An example I use to use:

https://www.adoptapet.com/public/apis/pet_list.html

When using an api such as that or paypal or google maps, etc, they will have instructions to follow. You follow their instructions for usage.

If you create an api you provide the instructions for users to follow.

https://laravel.com/docs/6.x/eloquent-resources

But API is also broader than that. Technically MySql is an api to php.

Application Programming Interface. But my above top answer is usually what people are referring to when they say api.

cyprous's avatar

So what is the common approach for building Laravel apps based on Blade templating system?

Internal API or just Eloquent Models?

Is it a bad practice to use both and mix some Vue.js components to help with reactivity?

I cannot get my head around whether I should pass some data to blade view via compact, render it using blades directives or pass it as props for inline template by Vue or let Vue handle it all (template, and fetching data) which essentially equals making another call to server as soon as the initial one has finished...

My fear is - Vue setup will require API resources (or internal API resources), while without Vue, I will be able to manage with simple Eloquent Models returns?

I like Vue, but it adds extra complexity, that blade has got already covered such as roles and permission of what user can and cannot see.

henriquesalvan's avatar

Being honest with you, take the simplest way to get your app working.

When I speak simpler, use blade with controllers / eloquent models, and only use VueJS for things that need reactivity. Using VueJS only when you really need it, you'll be able to harness the full power of the blade and the things laravel already offers out of the box.

Only when you have total confidence on "VueJS + API Resources" you shold move foward and start making more complex things with this approat.

Some laravel open source projects: https://vegibit.com/25-popular-applications-built-with-laravel

I like this one: https://github.com/monicahq/monica

jlrdw's avatar

An API is needed when you are only interfacing. Let's say you program a forum, and it's a web application, that would not be an API as defined above.

On the other hand look at PayPal, when you use the API you do not have all of PayPal's programming code in your program, rather your program is interfacing with a set of standards that they provide.

So an API is needed when you are only interfacing the software.

And sorry I'm not super good at explaining this stuff.

I guess put another way an API is used to interface someone else's system like Google Maps.

It's their system, but your users through some API code of Google can interface with it and can find a location.

Edit: many of the applications that folks are calling an API, as defined above, probably is just a web application that they've turned into an API like interface, but not needed.

Please or to participate in this conversation.