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

gidaban79's avatar

Api results Resource or json?

Hello Guys hope you are oaky.

I am wondering about best practice about return data in api requests?

So far i make Vue component for display images in gallery without refreshing, however how it will be better?

User resource for return data or

return response()->json() 
0 likes
5 replies
tykus's avatar

How you return the response is less relevant to how you organise and structure the data; personally I prefer to use either Transformers or API Resources over returning the Eloquent model directly.

gidaban79's avatar

@tykus i just want use best practices, then use resource will be better option ?

tykus's avatar

i just want use best practices

Don't we all... but who decides on what is best practice?

Go with something you understand and can maintain. If you are using an API Resource, or similar well-used open source package, you are getting the benefit of countless hours of development and real-world testing.

gidaban79's avatar

So far, in my project i am using API for display images in gallery as back-end and vue for front-end.

Then basically in this case i don't need a API resource ?

tykus's avatar
tykus
Best Answer
Level 104

It is largely irrelevant how you use the resource on the frontend; but, if your frontend needs JSON, then a consistent API does matter. In that case, the API Resource makes perfect sense; but, if your needs are really so simple, and unlikely to become more complex, you can simple build the array representation in the controller:

$images = // some eloquent query
return response()->json(
    $images->map(function ($image) {
        return [
            'id' => $image->id,
            'path' => $image->path,
            'caption' => $image->caption,
            // ... 
        ];
    })
);

Please or to participate in this conversation.