Closed beonami closed 2 years ago
Some time later I tried with a different URL, entering the user_id to fields[posts].
http://laravel.local:8083/api/posts?fields[posts]=id,user_id,body&fields[author]=id,name&include=author
...
Response: {
"data": [
{
"id": 1,
"user_id": 1,
"body": "Lorem ipsum dolor sit ame.",
"author": {
"id": 1,
"name": "Fahmi Ramdhani",
"email": "fahmi@laravel.local",
"email_verified_at": null,
"created_at": "2022-07-23T02:41:07.000000Z",
"updated_at": "2022-07-23T02:41:07.000000Z"
}
}
],
The select feature does not match the requested parameters, the author field is displayed all.
Apparently, the problem is from the fields[relation?] parameter. Where the query builder package or laravel specify the name / alias plural. So there is a difference in the Controller.
App\Models\Post.php
...
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
But in controller and selection, allowedFields must be written in plural. allowedFields[authors], NOT allowedFields[author]
...Controllers\Api\PostController.php
$posts = QueryBuilder::for(Post::class)
->allowedFields([
'id', 'user_id', 'body',
'authors.id', 'authors.name',
])
->allowedIncludes(['author'])
->jsonPaginate();
return PostResource::collection($posts);
Request: GET http://kinetik.local:8083/api/posts?fields[posts]=id,user_id,body&fields[authors]=id,name&include=author
...
Response: {
"data": [
{
"id": 1,
"user_id": 1,
"body": "Lorem ipsum dolor sit ame.",
"author": {
"id": 1,
"name": "Fahmi Ramdhani"
}
}
],
...
I am confused by the results that do not match what is documented. I'am using the package version 5.0.2.
Based on the information above, the response from the API is:
Please help, what's wrong? I redo the documentation but the result is the same. Thank you.