Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thewebartisan7's avatar

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

0 likes
2 replies
martinbean's avatar

@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?

thewebartisan7's avatar

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.