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

trevorpan's avatar

When is a JSON API necessary in a Laravel app?

When you type JSON API and Laravel into a search engine there's tons of posts on how to build one—even the virtues of building one. I could not find one that stated why, or when to use one. e.g. when your app does x, it "must" use a JSON API.

I watched the Laracasts API Resources video and it seems great, but I'm not fully understanding the difference of retrieving database info as JSON vs what a Controller will do (without ResourceCollections).

https://www.youtube.com/watch?v=7YcW25PHnAA this video shows data rendered as webpages (with styling), but also JSON info in the background if you use the right api web address. e.g. api.twitter.com

I'm having a hard time figuring out if it's necessary in my app. Below is a attempt at using a ResourceCollection, after checking out Laravel Up and Running 2.0 v2 (book).

//Routes\web.php

use App\Job;
use App\Http\Resources\Job as JobResource;

Route::get('jobs/{id}', function ($id){
             return new JobResource(Job::find($id));
        });

The above spits out (without showing the website view):

data    
    id   1
    jobtitle    "Test"
    body    "test"
    projectaddress  "123"
    city    "phoenix"
    state   "arizona"
    zipcode 85004
    biddertype  "subcontractor"
    job "materials"
    subjob  "mechanical"
    deadline"2019-05-13 16:26:58"

If a poster posted a job, and I wanted it to then be posted to say Twitter, or LinkedIn (for promotion), is it mandatory to use the JSON API resource method?

Can this feature be accomplished with just controllers?

0 likes
5 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

If a poster posted a job, and I wanted it to then be posted to say Twitter, or LinkedIn (for promotion), is it mandatory to use the JSON API resource method?

It depends on the api. Each service, Twitter, LinkedIn, Jobs are us, Billy Bob's jobs, etc will all have a set of specs you abide by. Some may require xml, some csv, etc.

Json has kind of become the de facto.

An example:

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

Xml and Json examples.

trevorpan's avatar

First off, cool example!

So, if an app "interacts" with another website, "then" it needs an API?

If your website does not interact with a website, e.g. pull in pets, or I imagine, add pets to their selection (in your example) then doing an API is unnecessary?

jlrdw's avatar

Take this forum for example, I imagine all data comes from same server. So just normal queries can retrieve the data.

But in your example, you are sending data for their server and or database to display. So yes it's not like they are doing a query from your database, they are just getting the data results for them to display.

Once they have it, I imagine it just goes into their database and a normal query is used to display the data.

Show me all employee candidates that can weld aluminum.
1 like
jlrdw's avatar

Just remember each api will have specs, but you can look over some to learn, like Fedex, UPS, etc.

And take some Json tutorials. God speed.

trevorpan's avatar

Yea, definitely reading a lot. It's crazy all this stuff. I've thumbed through the JSONapi website, and took a brief look at the laravel.jsonapi site, too.

You really nailed it though, the need of JSON API hinges on using other sites or interacting with them. Somehow I missed that in all the articles.

Please or to participate in this conversation.