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

wivaku's avatar

use model through external REST API

I can use a Laravel model with an external database (by using protected $connection = ...). Can I achieve something similar for an external REST API?

This so I can use Eloquent as much as possible. E.g. Movies::where('name','Papillon') which would call e.g. imdb REST API.

I know I could use e.g. this to do specific REST API calls: Http::get('https://reqres.in/api/users')->getBody()->getContents()

I would like to treat all this as much as a regular Laravel model as possible.

0 likes
3 replies
automica's avatar

@wivaku rather than creating a model, you should be looking at creating a Repository to consume your external API and then you can define your methods such as list, get, where etc

see:

Once you have data back from your api, you'll want transform it before using it as you won't necessary want to expose the data in its raw form.

Laravel has this transformation layer you can use:

This article is also a good read:

1 like
wivaku's avatar

Thanks for the food for thought!

The Repository Pattern is definitely worth looking into. The Resources is I think targeted at the "last-mile": converting to array right before outputting an API?

Please or to participate in this conversation.