Bind Laravel API Resources Interface
How to bind a laravel API Resources Interface like we can do with Request?
So in my service provider I would do something like:
$this->app->bind(\App\Contracts\Resources\UserInterface::class, \App\Http\Resources\UserResource::class);
But in my controller I am not sure how to inject or call the interface, I try different way but don't work.
Thanks
@zoroaster Why are you trying to inject a resource? You should be wrapping an object/collection of objects in a resource:
public function show(Product $product)
{
return new ProductResource($product);
}
If you inject the resource class in the above example, how is it supposed to know which product object to wrap?
Hi Martin, thanks for your reply.
I am working with a package, and I want to allow override of resource without changing controller.
I know that I can't inject since it require the object, but maybe I can do with app(ResourceInterface::class) or app()->make()?
I try different way as explained in docs https://laravel.com/docs/5.8/container but can't sort this.
If there is not way, I have another way.
Thanks
Please or to participate in this conversation.