PHP code example of petrelli / eloquent-consumer

1. Go to this page and download the library: Download petrelli/eloquent-consumer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petrelli / eloquent-consumer example snippets



// 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']);




// ...
class Book extends ApiModel
{
    //...

    public function scopePublished($query)
    {
        return $query->rawQuery(['published' => true]);
    }

    //...
}

// Call to the published scope and then call to the collection endpoint
\App\Book::query()->published()->get();



namespace App\ApiConsumer\Endpoints;

use \Petrelli\EloquentConsumer\Endpoints\BaseEndpoint;

class Main extends BaseEndpoint
{

    protected $baseUri = 'https://baseapi.com';

    protected $defaultTTL = 200;


}



namespace App;

use \Petrelli\EloquentConsumer\Models\ApiModel;

class Book extends ApiModel
{

    protected $endpointClass = \App\ApiConsumer\Endpoints\Main::class;

    protected $endpoints = [
        'collection' => '/books',
        'resource'   => '/books/{id}',
    ];

}


use \Petrelli\EloquentConsumer\Endpoints\BaseEndpoint;

class MainEndpoint extends BaseEndpoint
{

    //Mandadory if not added at the general config file
    //This will be your baseline URL. E.g. http://apibase.com
    protected $baseUri;

    //Mandadory if not added at the general config file. Number of seconds each call will be cached.
    protected $defaultTTL;

    //Custom Grammar or Consumer classes
    protected $grammarClass;
    protected $consumerClass;

    //......
}

protected function compileSearchText($query, $text)
{
    if ($text)
        return ['q' => $text];
    else
        return [];
}



namespace App\ApiConsumer\Endpoints;

use \Petrelli\EloquentConsumer\Endpoints\BaseEndpoint;

class Base extends BaseEndpoint
{

    protected $baseUri = 'https://baseapi.com';

    protected $defaultTTL = 200;


}

protected $connectionClass = \App\ApiConsumer\Grammar\MyOwnGrammar::class;



namespace App\ApiConsumer\Grammar;

use \Petrelli\EloquentConsumer\Grammar\BaseGrammar;

class MyOwnGrammar extends BaseGrammar
{

    protected function compilePage($query, $page)
    {
        return ['page_number' => $page];
    }

}


// 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']);

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




// ...
class Book extends ApiModel
{
    //...

    public function scopePublished($query)
    {
        return $query->rawQuery(['published' => true]);
    }

    //...
}

// Call to the published scope and then call to the collection endpoint
\App\Book::query()->published()->get();



namespace App\ApiConsumer\Transformers;

use Petrelli\EloquentConsumer\Transformers\BaseTransformer;


class Base extends BaseTransformer
{

    /**
     * Transform Grants API response to a format we can read
     */

    public function transform()
    {
        $original = $this->response->body;

        $original->pagination = (object) [
         'total' => $original->recordsTotal,
         'page'  => $original->draw,
        ];

        return $this->response;

    }


}

php eloquent-consumer:endpoint Main

php artisan vendor:publish --provider="Petrelli\EloquentConsumer\EloquentConsumerServiceProvider"

php artisan eloquent-consumer:endpoint Base

php artisan eloquent-consumer:endpoint EndpointName Namespace1/Namespace2