Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amsoell/withable-sortable
A Laravel package to add eager loading and sorting to API endpoints
https://github.com/amsoell/withable-sortable
Last synced: about 6 hours ago
JSON representation
A Laravel package to add eager loading and sorting to API endpoints
- Host: GitHub
- URL: https://github.com/amsoell/withable-sortable
- Owner: amsoell
- License: mit
- Created: 2022-03-02T11:04:57.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-03-02T11:09:20.000Z (almost 3 years ago)
- Last Synced: 2025-01-03T01:08:38.285Z (2 days ago)
- Language: PHP
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel withable-sortable
[![Latest Version on Packagist](https://img.shields.io/packagist/v/amsoell/withable-sortable.svg?style=flat-square)](https://packagist.org/packages/amsoell/withable-sortable)
[![Total Downloads](https://img.shields.io/packagist/dt/amsoell/withable-sortable.svg?style=flat-square)](https://packagist.org/packages/amsoell/withable-sortable)This Laravel package enables dynamic eager loading and sorting in your API controllers.
## Installation
You can install the package via composer:
```bash
composer require amsoell/withable-sortable
```## Usage
Add the `withable()` and `sortable()` calls on any Eloquent queries in your API controllers to automatically enable eager loading and sorting through querystring parameters. An controllerless example:
```php
use Illuminate\Support\Facades\Route;Route::get('/users', function () {
$users = User::query()
->sortable()
->withable();return $users->paginate()->withQueryString();
});
```- `/users` will return users sorted by the default (`created_at` in ascending order)
- `/users?sort=email` will return users sorted by email address
- `/users?sort=email&direction=desc` will return users sorted by email in descending order
- `/users?with=posts` will return users with a `posts` relationship eager loaded
- `/users?with[]=posts&with[]=comments` will return users with `posts` and `comments` relationships both eager loadedIf you want to specifically eager load some relationships while allowing additional eager loads via `with=`, you can specify them in your route method:
```php
$users = User::query()->withable([
'posts',
]);
```You can also set the default sort parameters:
```php
$users = User::query()->sortable([
'updated_at',
'asc',
]);
```Even when setting default eager loads or sorts, they can be added to or overridden via querystring paramters.
### 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.
## Credits
- [Andy Soell](https://github.com/amsoell)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
## Laravel Package Boilerplate
This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).