laravel include api
English | 简体中文
tree-ql is a laravel extension that builds a highly descriptive, readable, high-performance API with no redundancy through a simple configuration.
Will not invade laravel, can be integrated into the existing laravel project at any time.
Will not invade RESTful and further improve API descriptiveness based on RESTful.
No matter how complex the API description, no N + 1 problem will occur.
Tree-ql is not a set of API specifications, but a production environment solution that can improve development efficiency.
WYSIWYG, the client can include the required resources according to their own needs without redundancy.
Make sure your laravel version is above 5.5 and execute in the project root directory
composer require weiwenhao/tree-qlThe current version number CustomUserResource::class, ];
UserResource definition
```php
# UserResource.php
belongsTo(User::class);
}
}
…
json
{
"id": 1,
"title": "...",
"liked": true,
}The client likes this easy-to-bind data structure, but there is no liked field in the post table to record whether you liked the post.
At this point custom has come in handy, as defined in $custom above. But what's worse?
protected $custom = [
'liked' => function ($post) {
// logic
return $bool;
}
];Yes, we still have a callback to determine whether the current login user liked the post. But php does not support the above. So in tree-ql we need to pull the callback out.
protected $custom = [
'liked'
];
public function liked($post, $params)
{
// logic
// return $bool;
}
…
json
{
"data": [
{
"id": 1,
"title": "Aperiam quisquam porro fugiat et in itaque",
"user_id": 1
},
],
"meta": {
"pagination": {
"per_page": 15,
"total": 100,
"current": 1,
"next": "http://test.com/api/posts?page=2",
"previous": null,
"last": 7
}
}
}
…
php
protected $custom = [
'liked'
];
protected $meta = [
'test'
];
/**
* @param $post
* @param $params ["key1" => "value1", "key2" => "value2"]
*/
public function liked($post, $params)
{
// logic
// return
}
/**
* @param $params ["key1" => "value1", "key2" => "value2"]
*/
public function test($params)
{
// logic
// return
}There is also a type of situation where relational params. relation load is responsible for tree-ql, and there is no custom callback function. But in load, the corresponding callback is reserved.
For example, test.com/api/posts/{post}?include=comments(sort_by:floor) then the corresponding callback is
# CommentResource.php
[
'resource' => CommentReplyResource::class,
]
];
/**
* test.com/api/posts/{post}?include=comments(sort_by:floor)
*
* sound code ↓ ↓ ↓
*
* $posts->load(['comments' => function ($builder) {
* $this->loadConstraint($builder, ['sort_by' => 'floor']);
* });
*
* ↓ ↓ ↓
*
* @param $builder
* @param array $params
*/
public function loadConstraint($builder, $params)
{
isset($params['sort_by']) && $builder->orderBy($params['sort_by'], 'desc');
}
}
The grammar rules of include and all the configuration rules of tree-ql have been introduced. Although it takes a long time to introduce, most of the space is used to introduce the use of the scene, the actual configuration is very simple.
Take a look at PostController, the use of tree-ql is clear at a glance.
/**
* test.com/api/posts?include=xxx
* Just pass the posts to PostResource, and the definitions and configurations are activated!!
*
* @return \Weiwenhao\TreeQL\Resource
*/
public function index()
{
// $posts = Post::columns()->latest()->get(); Same support
$posts = Post::columns()->latest()->paginate();
// Equivalent to return PostResource::make($post, request('include'))
return PostResource::make($posts);
}
/**
* @param \App\Models\Post $post
* @return \Weiwenhao\TreeQL\Resource
*/
public function show($post)
{
return $resource = PostResource::make($post);
}The only thing to explain here is the column() query constructor, which is provided by tree-ql. It will add the appropriate select() to the Builder according to the column and default defined in PostResource, combined with the actual include, instead of select *
This project is licensed under the MIT License
No open issues yet, or sync has not completed.