Framework: [Bug] BindingResolutionException: Unresolvable dependency resolving

Created on 15 Jan 2013  Â·  10Comments  Â·  Source: laravel/framework

I'm trying to use dependency injection as explained in the docs. My controller has the following construct :

<?php
class Projects extends BaseController
{
  public function __construct(Project $projects)
  {
    $this->projects = $projects;
  }

And here is the Project model :

<?php
class Project extends Eloquent {}

In my head this was supposed to work but somehow here is the Exception that is thrown :

BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ array $attributes = Array ]].
in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 327
at Container->getDependencies(array(object(ReflectionParameter))) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 301
at Container->build('Project', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 218
at Container->make('Project', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 261
at Application->make('Project') in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 330
at Container->getDependencies(array(object(ReflectionParameter))) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 301
at Container->build('Projects', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 218
at Container->make('Projects', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 261
at Application->make('Projects') in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Router.php line 573
at Router->Illuminate\Routing\{closure}()
at call_user_func_array(object(Closure), array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Route.php line 64
at Route->callCallable() in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Route.php line 39
at Route->run(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Router.php line 658
at Router->dispatch(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 369
at Application->dispatch(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 349
at Application->run() in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\public\index.php line 67

Can't Eloquent models be injected automatically like this ?

Most helpful comment

You should bind the class to $app, try adding this to routes.php:

App::bind('Project', 'Project');

or

App::bind('Project', function() { return new Project; });

All 10 comments

You should bind the class to $app, try adding this to routes.php:

App::bind('Project', 'Project');

or

App::bind('Project', function() { return new Project; });

First solution didn't work (because I guess it's what Laravel does in the background by default anyway). Second one did. Thanks !
Still pretty weird though, the Model constructor doesn't have anything in particular that may be hard to create or anything, it just hydrates a Model with an empty array, no external dependency no nothing, it's weird.

Looks like it ignores the fact that the $attributes parameter is optional. I'll take a look whether this can be fixed.

You wouldn't inject an Eloquent object like that. It wouldn't make sense. Inject a repository or something else. There would no point in injecting a model.

Well I'm not really trying to inject an Eloquent object, I'm trying to bind Eloquent as a repository — by using its query builder methods like you would use methods from a repository to fetch from the database.

Yeah, I wouldn't suggest that. You need to write a real repository if you want to get the testability benefits.

Anyways, this isn't really a "bug" per se. The IoC container needs to know what you want to inject because there is no type hint. I can probably improve the container to inject default arguments though. I believe Dan Horrigan added a pull for that.

What do you mean by there is not type hint ? In my controller or in the Model class ?

In the Model class.

Oh okay :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SachinAgarwal1337 picture SachinAgarwal1337  Â·  3Comments

shopblocks picture shopblocks  Â·  3Comments

digirew picture digirew  Â·  3Comments

RomainSauvaire picture RomainSauvaire  Â·  3Comments

YannPl picture YannPl  Â·  3Comments