PHPackages                             petrelli/eloquent-consumer - 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. [Database &amp; ORM](/categories/database)
4. /
5. petrelli/eloquent-consumer

ActiveLibrary[Database &amp; ORM](/categories/database)

petrelli/eloquent-consumer
==========================

API consumer and query builder that uses Eloquent like syntax

v0.0.5-alpha(7y ago)62763MITPHPPHP ^7.0

Since Aug 31Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ferpetrelli/eloquent-consumer)[ Packagist](https://packagist.org/packages/petrelli/eloquent-consumer)[ RSS](/packages/petrelli-eloquent-consumer/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

About
=====

[](#about)

Eloquent API Consumer will allow you to solve two main problems regarding API's in Laravel:

1. Generate and execute API calls in a clear and simple way
2. Process API responses and generate Eloquent like models and collections (including paginated ones)

You can see an example application here: [Eloquent Consumer Example APP](https://github.com/ferpetrelli/eloquent-consumer-test).

Development Note
----------------

[](#development-note)

This package is still on alpha state, and under heavy development. Things might change in the near future.

Motivation to create this package
---------------------------------

[](#motivation-to-create-this-package)

Our CMS was designed to use Eloquent like models as the data source.

So we needed a way to integrate an API origin to build some listings in a seamless way, without having to modify the CMS.

This library creates models who's interface will be compatible with Eloquent, so building queries, pagination, scopes, filtering, and mostly everything related to Eloquent will be available.

With this package you will be able to manage your API endpoints, caching strategies, low level API query configuration, model attributes, scopes and functions, and basically every element involved in the process of generating a query, to getting the processed end result.

Table of contents
=================

[](#table-of-contents)

- [Overview](#overview)
- [Core Concepts](#core-concepts)

    - [Endpoint](#endpoints)
    - [Consumer](#consumers)
    - [Grammar](#grammar)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Extended Reference](#extended-reference)
- [License](#license)

Overview
========

[](#overview)

Let's imagine we have a `Book` API model and we want to read some data from the API. Let's perform some calls right now:

```
// Call to the collection endpoint, and return a collection of Books
\App\Book::query()->get();

// Call to the collection endpoint, and return a paginated collection of 10 elements
\App\Book::query()->paginate(10);

// Call to the resource endpoint, {id} will be replaced with the value of $id and will return a Test object.
\App\Book::query()->find($id);

// Call to the resource endpoint, {id} will be replaced with the value of $id and will return a Test object. Throw a 404 if not found.
\App\Book::query()->findOrFail($id);

// Let's get a collection of search results for a term. This will simply add a 'q=Julio Cortazar' parameter to the query by default.
\App\Book::query()->search('Julio Cortazar')->get()

// Call to the collection endpoint, and let's just add a customized parameter to this call to search books by ISBN.
\App\Book::query()->rawQuery(['ISBN' => 123456])->get()

// Call to the collection endpoint, and return a collection of Books with only id, and title columns
\App\Book::query()->get(['id', 'title']);
```

As you can see this syntax is very familiar.

Let's say you want to add your own Eloquent like scope to get only published elements. Just follow the same syntax as you usually do with Laravel:

```
