Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mawuva/laravel-repository-layer

Repository Pattern implementation for Laravel
https://github.com/mawuva/laravel-repository-layer

Last synced: 7 days ago
JSON representation

Repository Pattern implementation for Laravel

Awesome Lists containing this project

README

        

# Repository Pattern implementation for Laravel

This package is a Simple Repository Pattern implementation for Laravel Projects and
an easily way to build Eloquent queries from API requests

## Installation

You can install the package via composer:

```bash
composer require mawuekom/laravel-repository-layer
```

### Laravel

After register the service provider to the **`providers`** array in **`config/app.php`**

```php
'providers' =>
...
Mawuekom\RepositoryLayer\RepositoryLayerServiceProvider::class
...
];
```

Publish package config

```bash
php artisan vendor:publish --provider="Mawuekom\RepositoryLayer\RepositoryLayerServiceProvider"
```

### Lumen

Go to **`bootstrap/app.php`**, and add this in the specified key

```php
$app->register(Mawuekom\RepositoryLayer\RepositoryLayerServiceProvider::class);

```

Then, create **`config`** folder in your root directory

Once done, create **`query-builder.php`** in the config folder and add this config in it

```php
[
'include' => 'include',

'filter' => 'filter',

'sort' => 'sort',

'fields' => 'fields',

'append' => 'append',
],

/*
* Related model counts are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsCount
*/
'count_suffix' => 'Count',

/*
* By default the package will throw an `InvalidFilterQuery` exception when a filter in the
* URL is not allowed in the `allowedFilters()` method.
*/
'disable_invalid_filter_query_exception' => false,

/*
* By default the package inspects query string of request using $request->query().
* You can change this behavior to inspect the request body using $request->input()
* by setting this value to `body`.
*
* Possible values: `query_string`, `body`
*/
'request_data_source' => 'query_string',
];

```

Create also **`json-api-paginate.php`** in the config folder and add this config in it

```php
30,

/*
* The default number of results that will be returned
* when using the JSON API paginator.
*/
'default_size' => 30,

/*
* The key of the page[x] query string parameter for page number.
*/
'number_parameter' => 'number',

/*
* The key of the page[x] query string parameter for page size.
*/
'size_parameter' => 'size',

/*
* The name of the macro that is added to the Eloquent query builder.
*/
'method_name' => 'jsonPaginate',

/*
* If you only need to display Next and Previous links, you may use
* simple pagination to perform a more efficient query.
*/
'use_simple_pagination' => false,

/*
* Here you can override the base url to be used in the link items.
*/
'base_url' => null,

/*
* The name of the query parameter used for pagination
*/
'pagination_parameter' => 'page',
];

```

Once done all of this, go back to **`bootstrap/app.php`**, and add the config files you created

```php
$app->configure('query-builder');
$app->configure('json-api-paginate');
```

This config allows you to filter, sort and include eloquent relations based on API requests.


It also allows to paginate and display data with the JSON API spec.

It using :

- [Laravel-query-builder](https://spatie.be/docs/laravel-query-builder/v3/introduction) to build queries

- [laravel-json-api-paginate](https://github.com/spatie/laravel-json-api-paginate) that plays nice with the JSON API spec



Check on this links for more informations


## Usage

This package has two repositories classes :
- **`BaseRepository`** which implements common methods for eloquent model
- **`BaseApiRepository`** that extends from `BaseRepository` and implements additional methods to build Eloquent queries from API requests

### Using **`BaseRepository`**

Your repository will look like this

```php

### Testing

```bash
composer test
```

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.