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

lirone's avatar

Make a restful API for fetching data from database ?

I'm learning Laravel. What I would like to know if fetching data from my database for feeding a map (leaflet JS)

Let's say, I have a table called Location which contains all the location (id, name, latitude, longitude).

So far, what I want to do is the following:

  • Create LocationController
  • Create LocationRepositories that link my controller to my model Location. Inside, I will have the all() method that will fetch all my location rows.
  • In my index function, call the LocationRepositories::all() to fetch the data and pass them to my View.

But I saw that there is another way to do it using axio i.e fetch data from API. For example axios.get('{{ route('api.location.index') }}') For example the url /api/locations would returns a json that contains all my locations data. So this time, my LocationController will be a ressource and I will have a Location API in my API Folder.

What is the recommended way to do things ? Is doing API for fetching data is a better practice ?

0 likes
1 reply
lirone's avatar

Up

What is the advantage from fetching directly from the DB or fetching from the DB via an api ?

Please or to participate in this conversation.