Skip to content

A Laravel package to add eager loading and sorting to API endpoints

License

Notifications You must be signed in to change notification settings

amsoell/withable-sortable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel withable-sortable

Latest Version on Packagist Total Downloads

This Laravel package enables dynamic eager loading and sorting in your API controllers.

Installation

You can install the package via composer:

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:

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 loaded

If you want to specifically eager load some relationships while allowing additional eager loads via with=, you can specify them in your route method:

$users = User::query()->withable([
    'posts',
]);

You can also set the default sort parameters:

$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 for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

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

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

About

A Laravel package to add eager loading and sorting to API endpoints

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages