14

I have an Eloquent Model called Surface which is dependent on a ZipCodeRepository object:

class Surface extends Model{
    public function __construct(ZipCodeRepositoryInterface $zipCode){...}

and an Address object that hasMany surfaces.

class Address extends Model{
    public surfaces() { return $this->hasMany('App/Surface'); }
}

My issue is when I call $address->surfaces I get the following error:

Argument 1 passed to App\Surface::__construct() must be an instance of App\Repositories\ZipCodeRepositoryInterface, none given

I thought the IoC would automatically inject that.

1

3 Answers 3

30

Thanks to @svmm for referencing the question mentioned in the comments. I found that you cannot use dependency injection on Models because you would have to change the signature on the constructor which doesn't work with the Eloquent framework.

What I did as an intermediate step, while refactoring the code, is use App::make in the constructor to create the object, such as:

class Surface extends Model{
    public function __construct()
    {
        $this->zipCode = App::make('App\Repositories\ZipCodeRepositoryInterface');
    }

That way the IoC will still grab the implemented repository. I am only doing this until I can pull the functions into the repository to remove the dependency.

2
  • Thank you! When passing in a dependency to the Model constructor, I was getting a 'too few arguments' error when performing an Eloquent find(id) . This method works much better
    – Stetzon
    Commented Mar 8, 2017 at 2:40
  • Thank you, after 1h of searching for a solution, this was the way to solve it. Commented Sep 24, 2019 at 6:52
9

In Laravel 5.7 you can use the global resolve(...) method. I don't think the global App is defined in more recent version of Laravel.

$myService = resolve(ServiceName::class);

Resolving in Laravel docs

2

However it might not be a good practice to inject services into your models either by constructor or method injection, think about designing the system in such a way that you do not need to do that and instead maybe inject the model into a service.

Let's see an example(just a dummy example in order to get to the point!).

  • Say we have Basket and Order models, and we want to add orders to basket
  • And we have a discount service that calculates discount based on orders
  • Every time user adds an order to basket we need to calculate new discount and set it on basket

one approach is:

class OrderController
{
    function store(User $user, Order $order)
    {
        $basket = $user->getBasket();
        $basket->addOrder($order);
    }
}

class Basket
{
    private $discountService;

    public function __construct(DiscountService $discountService)
    {
        $this->discountService = $discountService;
    }

    function addOrder(Order $order)
    {
        $this->orders[] = $order;
        $discount = $this->discountService->calculateFor($this->orders);
        $this->discount = $discount;
    }
}

class DiscountService
{
    function calculateFor(array $orders) {
        // code for calculating discount;
        return $discount;
    }
}

In this approach we injected discount service into Basket model

Another better approach would be like this:

class OrderController
{
    private $discountService;

    public function __construct(DiscountService $discountService)
    {
        $this->discountService = $discountService;
    }

    function store(User $user, Order $order)
    {
        $basket = $user->getBasket();
        $basket->addOrder($order);
        $this->discountService->setDiscount($basket);
    }
}

class Basket
{
    function addOrder(Order $order)
    {
        $this->orders[] = $order;
    }

    function getOrders()
    {
        return $this->orders;
    }

    function setDiscount(int $discount)
    {
        $this->discount = $discount;
    }
}

class DiscountService
{
    function setDiscount(Basket $basket) {
        $discount = $this->calculateFor($basket->getOrders());
        $basket->setDiscount($discount);
    }

    private function calculateFor(array $orders)
    {
        // code for calculating discount
        return $discount;
    }
}
  • In the first approach basket is making the decision about having discount, but this is not basket's concern
  • In the first approach basket depends on discount service, but in real world you don't need a discount service to have a basket
1
  • This answer also showed me how the need of mocking can reveal code smells. Commented May 26, 2021 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.