PHPackages                             pellerichard/laravel-dynamic-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. pellerichard/laravel-dynamic-api

ActiveLibrary[API Development](/categories/api)

pellerichard/laravel-dynamic-api
================================

Laravel Dynamic API

v1.1(3y ago)05MITPHPPHP &gt;=8.1

Since Oct 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/pellerichard/laravel-dynamic-api)[ Packagist](https://packagist.org/packages/pellerichard/laravel-dynamic-api)[ RSS](/packages/pellerichard-laravel-dynamic-api/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

**Laravel Dynamic API**
=======================

[](#laravel-dynamic-api)

Issue
-----

[](#issue)

Implementing multiple CRUD's with different table name and columns could take a lot of hours to make.

Solution
--------

[](#solution)

Dynamic API which holds the data within a generic table.

Pros:

- Saves a lot of time
- You can always set new type of data without touching the previous records
- Perfect for Micro sites / Micro services, and for small projects

Cons:

- Slower than standard solutions due to the data saving mechanism.

Installation
------------

[](#installation)

Install the package:

`composer require pellerichard/laravel-dynamic-api`

Publish the config:

`php artisan vendor:publish --provider="Pellerichard\LaravelDynamicApi\DynamicApiServiceProvider"`

Run the migration:

`php artisan migrate`

You should now have a `config/dynamic-api.php` file that allows you to configure the basics of this package.

Endpoint access:
----------------

[](#endpoint-access)

Default Endpoints:

Retrieve every entities:

> **GET** /api/v1/dynamic-api

Retrieve all entities within a table by table name:

> **GET** /api/v1/dynamic-api?type=people

Retrieve specific entity within a table by type and record\_id:

> **GET** /api/v1/dynamic-api?type=people&amp;record\_id=1

Create an entity:

> **POST** /api/v1/dynamic-api?type=people&amp;age=32&amp;height=180cm&amp;first\_name=John&amp;last\_name=Doe

Update a specific entity by type and record\_id:

> **PATCH** /api/v1/dynamic-api?type=people&amp;record\_id=1&amp;age=33

Manual access:
--------------

[](#manual-access)

```
use Pellerichard\LaravelDynamicApi\Services\Facades\ApiService;

// Create an entity.
ApiService::builder()
    ->setType(type: 'human')
    ->setAttributes(attributes: [
        'first_name' => 'Richárd',
        'last_name' => 'Pelle',
        'age' => 23,
        'occupation' => 'Full Stack Developer',
    ])
    ->create();

// Get all entities.
ApiService::builder()
    ->withPagination(withPagination: false)
    ->get();

// Get all entities with specific type.
ApiService::builder()
    ->whereType(type: 'human')
    ->withPagination(withPagination: false)
    ->get();

// Get one specific entity, and return it's data.
ApiService::builder()
    ->whereType(type: 'human')
    ->whereRecordId(recordId: 1)
    ->first();

// Update an entity.
ApiService::builder()
    ->whereType(type: 'human')
    ->whereRecordId(recordId: 1)
    ->setAttributes(attributes: [
        'age' => 24,
        'github_link' => 'https://github.com/pellerichard'
    ])
    ->update();

// Delete an entity.
ApiService::builder()
    ->whereType(type: 'human')
    ->whereRecordId(recordId: 1)
    ->delete();
```

### Available methods on Model

[](#available-methods-on-model)

> Method: getDataAsArray

Description: Returns the entity's data in array format.

### Customization options

[](#customization-options)

```
