Support the ongoing development of Laravel.io →
posted 10 years ago
IOC

Hi i'm having a bit of trouble trying to inject a class in laravel 5 in to another repository class. Basically I have this product class:

<?php namespace App\Repositories\Product;

use Illuminate\Database\Eloquent\Model;
use App\Repositories\Image\ImageRepositoryInterface;

class DbProductRepository extends Model implements ProductRepositoryInterface{

    
    protected $primaryKey = 'product_id';
    protected $table = 'product';
    protected $image;

    public function __construct(ImageRepositoryInterface $image)
    {
        $this->image = $image;
    }
}

I've set up the ImageRepository interface and created a bind in a service provider. Just when I try to use the product repository above with the image repository getting added in the __contstruct I get:

Argument 1 passed to App\Repositories\Product\DbProductRepository::__construct() must implement interface App\Repositories\Image\ImageRepositoryInterface, none given

I'm not sure how to fix that. Any ideas would be great!

Thanks very much

Last updated 3 years ago.
0

I guess it's a problem in your binding. Cant you show how you did to bind ImageRepositoryInterface to an implementation?

0

I can indeed. Here's how I bind it. It seems to work for other repo's and works for Image independently just not when injected to another repo. Thanks for the help

class AppServiceProvider extends ServiceProvider{

	public function register()
	{
		$this->app->bind(
			'App\Repositories\Product\ProductRepositoryInterface',
			'App\Repositories\Product\DbProductRepository'
		);
		$this->app->bind(
			'App\Repositories\ShopCategory\ShopCategoryRepositoryInterface',
			'App\Repositories\ShopCategory\DbShopCategoryRepository'
		);
		$this->app->bind(
			'App\Repositories\Image\ImageRepositoryInterface',
			'App\Repositories\Image\ImageRepository'
		);
	}
}
0

In theory, it should work... Maybe a typo, but I didn't find.

Anyway, you can try to bind your ImageRepository outside the service provider, something like this in your routes.php:

App::bind('App\Repositories\Image\ImageRepositoryInterface', function($app)
{
    return new App\Repositories\Image\ImageRepository;
});

or, try this in a test route, to make sure your bind is working:

Route:get('test', function(){
    $class = App::make('App\Repositories\Image\ImageRepositoryInterface');

    dd($class);
});
0

In theory, it should work... Maybe a typo, but I didn't find.

Anyway, you can try to bind your ImageRepository outside the service provider, something like this in your routes.php:

App::bind('App\Repositories\Image\ImageRepositoryInterface', function($app)
{
    return new App\Repositories\Image\ImageRepository;
});

or, try this in a test route, to make sure your bind is working:

Route:get('test', function(){
    $class = App::make('App\Repositories\Image\ImageRepositoryInterface');

    dd($class);
});
Last updated 10 years ago.
0

Unfortunately that doesn't work either. The only way I've found to do it is to inject the interface in to the controller and then pass that to the repository that requires it.

Seems like a poor way to do it though.

Cheers

0

Ok so I don't think it's ideal but calling the full repo namespace in the function that needs it has got it working so:


function needsImageClass()
{
    $image = new \App\Repositories\Image\ImageRepository();

}

If anyone has a better solution I'd love to hear it.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.