I want load relationship optionally on collection api end point
The end point will be something like
http://127.0.0.1:8000/api/posts/?include=comments includes comments with posts and I can add more using comma, like
http://127.0.0.1:8000/api/posts/?include=comments,images
but when I am not passing these query parameter, it should only return posts
with end point http://127.0.0.1:8000/api/posts
and http://127.0.0.1:8000/api/posts?page=10
RequestQueryFilter
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
class RequestQueryFilter
{
public function attach($resource, Request $request = null)
{
$request = $request ?? request();
return tap($resource, function($resource) use($request) {
$this->getRequestIncludes($request)->each(function($include) use($resource) {
$resource->load($include);
});
});
}
protected function getRequestIncludes(Request $request)
{
// return collect(data_get($request->input(), 'include', [])); //single relationship
return collect(array_map('trim', explode(',', data_get($request->input(), 'include', [])))); //multiple relationships
}
}
in Helper
<?php
if ( ! function_exists('filter') ) {
function filter($attach)
{
return app('filter')->attach($attach);
}
}
?>
In PostController
public funciton index(Request $request) {
$posts = Post::all();
return new PostCollection(filter($posts));
}
In PostCollection
return [
'data' => $this->collection->transform(function($post){
return [
'id' => $post->id,
'title' => $post->title,
'body' => $post->body,
'comments' => new CommentCollection($post->whenLoaded('comments')),
'images' => new ImageCollection($post->whenLoaded('images'))
];
}),
];
Showing
Call to undefined method App\Models\Post::whenLoaded()", but in if I am using single model resource, it is working fine.
Update: Cause:- post collection transform gives
Collection gives
Post {#363
#guarded: array:1 [
0 => "id"
]
#connection: "mysql"
#table: "posts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [
but Post resource $this
gives
Post {#344
+resource: Post {#343
#guarded: array:1 [
0 => "id"
]
#connection: "mysql"
#table: "posts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [
now converting PostCollection $post to PostResource
'data' => $this->collection->transform(function($post) use ($request) {
$post = new PostResource($post);
dd($post);
Using this
'comments' => new CommentCollection($post->whenLoaded('comments')),
will always return comments, even there is no comments
in include
$post->relationLoaded('comments')
returns true always.