PHPackages                             robertogallea/eloquent-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. robertogallea/eloquent-api

ActiveLibrary[API Development](/categories/api)

robertogallea/eloquent-api
==========================

Eloquent models backed by Rest API

1.1.1(3y ago)312564MITPHPPHP ^7.3|^7.4|^8CI failing

Since Mar 22Pushed 3y ago2 watchersCompare

[ Source](https://github.com/robertogallea/eloquent-api)[ Packagist](https://packagist.org/packages/robertogallea/eloquent-api)[ RSS](/packages/robertogallea-eloquent-api/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (6)Versions (5)Used By (0)

Eloquent Models for json APIs
=============================

[](#eloquent-models-for-json-apis)

### A package that lets you lay Eloquent on top of a json web API.

[](#a-package-that-lets-you-lay-eloquent-on-top-of-a-json-web-api)

This package provides an Eloquent model that sits on top of a json web API endpoint.

When you use this package, an initial invocation of the model will make one or more requests to an HTTP endpoint, data are read and converted in an array of rows. Each row is stored as a record in a table inside a file-based sqlite database. Subsequent invocations of the model use that sqlite database so changes underlying data won't be reflected in the database. However, there are two ways that you can invalidate the sqlite cache and cause it to be recreated:

1. You can call the `invalidateCache()` method on the model with something like `YourApiModel::first()->invalidateCache()`
2. By sending a request to a route provided by this package that deletes the sqlite database forcing a fresh load the next time the model is used.

### Installation

[](#installation)

```
composer require robertogallea/eloquent-api
```

### Usage

[](#usage)

Consider the following JSON response, provided by the endpoint `https://ghibliapi.herokuapp.com/films`

```
[
  {
    "id": "2baf70d1-42bb-4437-b551-e5fed5a87abe",
    "title": "Castle in the Sky",
    "description": "The orphan Sheeta inherited a mysterious crystal that links her to the mythical sky-kingdom of Laputa. With the help of resourceful Pazu and a rollicking band of sky pirates, she makes her way to the ruins of the once-great civilization. Sheeta and Pazu must outwit the evil Muska, who plans to use Laputa's science to make himself ruler of the world.",
    "director": "Hayao Miyazaki",
    "producer": "Isao Takahata",
    "release_date": "1986",
    "rt_score": "95",
    "people": [
        "https://ghibliapi.herokuapp.com/people/"
    ],
    "species": [
        "https://ghibliapi.herokuapp.com/species/af3910a6-429f-4c74-9ad5-dfe1c4aa04f2"
    ],
    "locations": [
        "https://ghibliapi.herokuapp.com/locations/"
    ],
    "vehicles": [
      "https://ghibliapi.herokuapp.com/vehicles/"
    ],
    "url": "https://ghibliapi.herokuapp.com/films/2baf70d1-42bb-4437-b551-e5fed5a87abe"
  },
  {
    "...": "..."
  }
]
```

We want to lay an Eloquent model on top of it.

```
php artisan make:api-model
```

**Step 1 - Enter the full path to the directory where you want to create the model file (defaults to `app_path()`):**

---

**Step 2 - Enter the name you want to use for your model class:**

---

**Step 3 - Paste the endpoint of your resource:**

---

**Step 4 - If your data is wrapped in a particular field, type its name:**

---

**Step 5 - If your resource is paginated, type the json field containing the url of the next page:**

---

**Step 6 - Confirm that the path and full classname look right:**

### The Resulting Model Class

[](#the-resulting-model-class)

```
use robertogallea\EloquentApi\ApiModel;

class YourApiModel extends ApiModel
{
    protected $endpoint = 'https://ghibliapi.herokuapp.com/films'; // The endpoint
    protected $nextPageField = null;
    protected $dataField = null;
}
```

### Dealing with wrapped resources

[](#dealing-with-wrapped-resources)

If your resource data is wrapped into a particular json key, you need to tell your model, what this key is, by setting the `$dataField` model attribute. For example

```
protected $dataField = 'data';
```

### Dealing with paginated data

[](#dealing-with-paginated-data)

If your endpoint provides paginated data, the package support multiple page fetching. In this case all you need to do, is setting the `$nextPageField` attribute as a string containing the name of the json field containing the next page link, for example

```
protected $nextPageField = 'next_page_url';
```

This model can do your basic Eloquent model stuff because it really is an Eloquent model. Though it's currently limited to read / list methods. Update and insert don't currently work.

Issues, Questions and Pull Requests
===================================

[](#issues-questions-and-pull-requests)

You can report issues and ask questions in the [issues section](https://github.com/robertogallea/eloquent-api/issues). Please start your issue with ISSUE: and your question with QUESTION:

If you have a question, check the closed issues first. Over time, I've been able to answer quite a few.

To submit a Pull Request, please fork this repository, create a new branch and commit your new/updated code in there. Then open a Pull Request from your new branch. Refer to [this guide](https://help.github.com/articles/about-pull-requests/) for more info.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 52.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~332 days

Total

4

Last Release

1244d ago

PHP version history (2 changes)1.0.0PHP ^7.3

1.1.1PHP ^7.3|^7.4|^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/818f547bcf73a82393d9014c85c90c83d760102a8d4dfe806353afb83848a901?d=identicon)[robertogallea](/maintainers/robertogallea)

---

Top Contributors

[![cnastasi](https://avatars.githubusercontent.com/u/2204693?v=4)](https://github.com/cnastasi "cnastasi (11 commits)")[![robertogallea](https://avatars.githubusercontent.com/u/19411470?v=4)](https://github.com/robertogallea "robertogallea (10 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/robertogallea-eloquent-api/health.svg)

```
[![Health](https://phpackages.com/badges/robertogallea-eloquent-api/health.svg)](https://phpackages.com/packages/robertogallea-eloquent-api)
```

###  Alternatives

[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3731.2M42](/packages/tencentcloud-tencentcloud-sdk-php)[convertkit/convertkitapi

Kit PHP SDK for the Kit API

2167.1k1](/packages/convertkit-convertkitapi)[mapado/rest-client-sdk

Rest Client SDK for hydra API

1125.9k2](/packages/mapado-rest-client-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
