Replies: 1 comment
-
You can do that as follows: /**
* Display the specified resource.
*
* @param \App\Models\Book $book
* @return \Illuminate\Http\Response
*/
public function show(Book $book)
{
return new BookResource(
QueryBuilder::for($book)
->allowedIncludes([])
->firstOrFail()
);
} Unfortunately, you'll send a duplicate query this way because you apply route model binding first. You could avoid this by omitting this model binding: /**
* Display the specified resource.
*
* @param \App\Models\Book $book
* @return \Illuminate\Http\Response
*/
public function show(int $book)
{
return new BookResource(
QueryBuilder::for(Book::class)
->where('id', $book)
->allowedIncludes([])
->firstOrFail()
);
} I haven't found a better way yet myself. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Looks like this packages is mostly focused on collections and doesn't pay much attention to single objects, but there is at least one case when it may be needed - for includes.
E.g. I would like to have an API to get one entity
/books/26?include=author
. How can I get it?Without includes it's simple:
But I don't want to reimplement includes functionality that already exists in this packages by myself.
Beta Was this translation helpful? Give feedback.
All reactions