IoC not being resolved automatically


IoC not being resolved automatically



I'm implementing repositories in my app. In my PresupuestoController (Presupuesto = Estimate in spanish) PresupuestoRepo gets injected in the constructor:


<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsPaciente;
use AppRepositoriesPresupuestoRepository;

class PresupuestosController extends Controller
{
protected $presupuestoRepo;

public function __construct(PresupuestoRepository $presupuestoRepo )
{
$this->presupuestoRepo = $presupuestoRepo;
}

/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create(Paciente $paciente)
{
$presupuesto = $this->presupuestoRepo->createNew($paciente);
dd($presupuesto);
return view('presupuesto.edit', compact('paciente'));
}
}



So far so god. It works. But now I need to inject a dependency in the PresupestoRepo itself, PacienteRepository (Paciente = Patient in spanish)


<?php

namespace AppRepositories;

use CarbonCarbon;
use AppModelsPresupuesto;
use AppRepositoriesPacienteRepository;

class PresupuestoRepository extends BaseRepository {

protected $pacienteRepo;

public function __constructor(PacienteRepository $pacienteRepo)
{
// THIS CONSTRUCTOR IS **NEVER** CALLED !!!

$this->pacienteRepo = $pacienteRepo;
dd($pacienteRepo); // here is the problem: pacienteRepo is NULL !!!
}

public function getRepo()
{
return $this->presupuestoRepo;
}
}



but PaceinteRepository is not automaticly invoqued, getting null instead.



For the sake of completeness , PacienteRepository:


<?php

namespace AppRepositories;

use AppModelsPaciente;
use AppModelsOdontograma;



class PacienteRepository extends BaseRepository {

public function __constructor(){}

/**
* @return string
*/
public function getClassName()
{
return 'AppModelsPaciente';
}
}



How to get a PacienteRepo injected in PresupuestoRepository's constructor?





Have you added your repository in AppServiceProviders.php?
– Gaurav Dave
Jan 30 at 12:10





@GauravDave Is it required? PresupuestoRepository is not registered and is working, being injected in PresupuestoController's constructor. What makes the difference? Sorry, i'm new to this and may be i'm missing sth.
– Carlos Mora
Jan 30 at 12:20






It is preferred, as it binds the interface with its implementation.
– Gaurav Dave
Jan 30 at 12:24





Repository is not a service provider, so it cannot be registered. My doubt is I'm not able to see the difference. PresupuestoRepository doen't need registration to be injected in the controller...
– Carlos Mora
Jan 30 at 12:31





Shame on me! __constructor instead of __construct. That was the problem,
– Carlos Mora
Jan 30 at 17:56









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API