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

cwinterweb's avatar

How to split up responsibilities when getting data from an API

I have an application that is based around an API. When a user logs in on my app, it gathers data from the API and merges it with my database. To gather all of this info from the API, I am forced to make multiple requests to get it all, and it takes too much time (let's say over 15 seconds) to make the user wait for it to happen. My app is built on Laravel and Vue, so I can make this into an ajax request. I'm thinking this is the best way.

I'm wondering how to structure this in my project. Let's say the data is a user's posts. I'm grabbing the user's new posts from the API, comparing it to the posts I have already stored, and creating posts in my database with the new posts from the API. Then updating the view with the posts in a paginated state.

Would I have a NewPostsController that handles this on its index method along with a method on my User model called something like "getNewPosts" or "mergeNewPosts"? I guess the ajax would fire that off and, once completed, get all of the posts for the user?

Here's what I'm thinking in pseudo-code:

// Http/NewPostsController.php
public function index()
{
    Auth::user()->mergeNewPosts();
}
// App/User.php
public function mergeNewPosts() {
    $posts = $client->get('/api/posts', {
            // params
    });

    // add to database
    $posts->each(function($post) {
        Post::create($post);
    });
}

In the front-end these would be called in sequence as ajax calls and render for the user when ready.

axios.get('/newposts')
.then(function() {
    axios.get('/posts', function(response) {
        this.posts = response.data;
    });
});

I'm more-so concerned with how these functions should be divided within Laravel and which classes should be responsible for what. Is a controller where this should be located or is there another pattern that works here?

I'm probably thinking out loud more than anything, but if anyone has suggestions I'm open to them. Thanks! I can provide more details if needed.

0 likes
2 replies
mabdullahsari's avatar
Level 16

The Repository pattern is probably what you're looking for. I'd highly recommend this video for an introduction: https://www.youtube.com/watch?v=93ZhGkFIwbA

If later down the road you decide to completely ditch the API and use Eloquent for everything for example, it will be as easy as just swapping the repositories in the IoC container. You won't even have to touch your controllers.

cwinterweb's avatar

The video was very helpful, thank you. I'm just wondering how I use both at the same time if I have the service container binding to my DbPostsRepository, but I also need the ApiPostsRepository? Aren't we supposed to type hint with the interface in the controller? This would cause it to always go to the DbPostsRepository.

public function __construct(PostsRepositoryInterface $postsRepository) {
    // will always be the db
}

 

EDIT:

Found the answer to my question here:

https://laravel.com/docs/5.8/container#contextual-binding

We can pass different implementations of an interface to different classes based on the class.

Please or to participate in this conversation.