apichef / laravel-request-to-eloquent
Easily translate request query string to Eloquent query.
v1.0.1
2021-03-28 17:26 UTC
Requires
- php: ~7.4 || ^8.0
- apichef/laravel-request-query-helper: ^2.1
Requires (Dev)
- laravel/legacy-factories: ^1.1
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-11-29 01:57:54 UTC
README
Easily translate request query string to Eloquent query.
Install
Via Composer
$ composer require apichef/laravel-request-to-eloquent
We use apichef/laravel-request-query-helper
package as a dependency. You can publish it's the config file with:
$ php artisan vendor:publish --provider="ApiChef\RequestQueryHelper\RequestQueryHelperServiceProvider"
Basic usage
Model class:
namespace App; use App\Comment; use App\Tag; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $dates = [ 'published_at', ]; public function comments() { return $this->hasMany(Comment::class); } public function tags() { return $this->belongsToMany(Tag::class); } public function scopeDraft(Builder $builder) { return $builder->whereNull('published_at'); } }
Request query class:
namespace App\Queries; use App\Post; use ApiChef\RequestToEloquent\QueryBuilderAbstract; use Illuminate\Http\Request; class PostListQuery extends QueryBuilderAbstract { protected function init(Request $request) { return Post::query(); } protected $availableIncludes = [ 'comments', 'tags', ]; protected $availableFilters = [ 'draft', ]; protected $availableSorts = [ 'published_at', ]; }
Controller:
namespace App\Http\Controllers; use App\User; use App\Queries\PostListQuery; class DashboardController extends Controller { public function index(PostListQuery $postListQuery) { return $postListQuery ->parseAllowedIncludes([ 'comments', 'tags', ]) ->get() ->toArray(); } }
Http request:
GET /posts?include=comments,tags&filter[draft]&sort=-published_at
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email milroy@outlook.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.