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
I guess it's a problem in your binding. Cant you show how you did to bind ImageRepositoryInterface to an implementation?
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'
);
}
}
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);
});
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);
});
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
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community