rcbytes / fetch
fetch your eloquent models
Requires
- php: >=5.3.0
- illuminate/support: ~5.1
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
This package is not auto-updated.
Last update: 2024-11-21 00:16:31 UTC
README
Query Eloquent
models easily. it is ideal for who uses Laravel as an API
Install
Via Composer
$ composer require rcbytes/fetch
On config/app.php
Service Provider:
rcbytes\fetch\FetchServiceProvider::class
Facade:
'Fetch' => rcbytes\fetch\Fetch::class
Usage
Controller
... class UserController extends Controller public function index(Request $request) { // your criteria will overwrite criteria from Request->where $where = [ "account_id" => $request->user()->account_id ]; return Fetch::all(App\User::query(), $where); } public function show($id) { // third parameter $key defaults to "id" return Fetch::one(App\User::query(), $id); } ...
Model:
class User implements AuthenticatableContract, CanResetPasswordContract { ... public function friends() { return $this->hasMany(\App\User::class); } public function posts() { return $this->hasMany(\App\Model\Post::class); } ... }
Request :
Get all Users
with their Posts
and Friends
GET /user?with=["posts", "friends"]
Get Users
with their Posts
and Friends
that age
equals 30
GET /user?with=["posts", "friends"]&where={age: 30}
Get Users
with their Posts
and their Comments
and Friends
that have at least one Friend
with age
greater than 30
GET /user?with=["posts.comments", "friends"]&where={"friends.age":[ ">", 30]}
Get User
of key = 1
with their Posts
, Friends
and Friends of Friends
GET /user/1?with=["posts", "friends.friends"]
Parameters:
- array
with
: It expects theModels
to have therelationship method
. Acceptsdot notation
, bothModels
come when used. eg.:?with:["posts.comments"]
.User::posts
andPost::comments
methods must exist - object|mixed
where
: It expects theModels
to have the queried columns. Acceptsdot notation
, queries the relationship, if found at least one that matches the criteria, it returns the Model. eg.:?where:{age: 30}
,?where:{age:[">", 30]}
,?where:{"friends.age":[">", 30]}
- integer
take
: takes this number ofModels
- integer
paginate
: also known asper page
- integer
page
: page number
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ to do
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email lab.rafamello@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.