I have a TransportationController which uses six repositories. The repositories are injected to the controller via constructor and method.


use Illuminate\Http\Request;
use App\Repositories\TransportationRepository;
use App\Repositories\TruckRepository;
use App\Repositories\AccountRepository;
use App\Repositories\SiteRepository;
use App\Repositories\EmployeeRepository;
use App\Repositories\EmployeeWageRepository;
use App\Http\Requests\TransportationRegistrationRequest;
use App\Http\Requests\TransportationFilterRequest;
use \Carbon\Carbon;

class TransportationController extends Controller
    protected $transportationRepo;
    public $errorHead = 5, $noOfRecordsPerPage = 15;

    public function __construct(TransportationRepository $transportationRepo)
        $this->transportationRepo = $transportationRepo;

     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(TransportationFilterRequest $request, TruckRepository $truckRepo, AccountRepository $accountRepo, SiteRepository $siteRpepo, EmployeeRepository $employeeRepo)
        //some filtering stuff

        $transportations = $this->transportationRepo->getTransportations($params, $noOfRecords);

        return view('transportations.list', [
                'accounts'          => $accountRepo->getAccounts(),
                'sites'             => $siteRpepo->getSites(),
                'trucks'            => $truckRepo->getTrucks(),
                'drivers'           => $employeeRepo->getEmployees(),
                'materials'         => $this->transportationRepo->getMaterials(),
                'transportations'   => $transportations,
                'rentTypes'         => $this->transportationRepo->rentTypes,
                'params'            => $params,
                'noOfRecords'       => $noOfRecords,
  • Is there any architecture faults in injecting many(about 5 or 6) repositories to a controller?
  • Is there any performance issue related to injecting many(about 5 or 6) repositories to a controller?
  • What are the difference in injecting repositories through constructor vs injecting through method?
  • What about injecting repositories in another repositories

1 Answer 1


I'll be curious to see if anyone else has an opinion here, but while we wait here is mine:


I don't see anything obviously wrong with this code. I presume that your view actually needs all of that data in order to generate all the necessary HTML/etc. If that is the case you only have two options anyway: inject all your repositories or completely rebuild the actual HTML page so you don't need so much data. Again, presuming that your page simply needs all this data on it, the only way to do that would be with a completely different architecture: namely, a front end application that hits up a variety of endpoints to get each piece of data separately and then builds the actual page. That will substantially simplify your controllers (presumably they would just need one repository each), but it would also mean more controllers and a more complicated front-end, so I don't really see that as being much of an improvement. All that to say this: If your page needs a lot of different "kinds" of data then you have no choice but to fetch lots of different "kinds" of data.


4 or 5 repositories doesn't seem like a terribly large number to me. You could always find out by doing some profiling. The best way to answer "does this cause performance problems" is by profiling and finding out how it actually performs. Laravel has some easy-to-add-on tools for that. As a (very) rough guideline, if your page can completely render in a tenth of a second or so, then it isn't terrible. Ultimately though, performance is relative to page load and other factors. A high traffic page obviously needs to have better performance than one visited once a month by a single person. In essence, poor performance + high traffic = crashed server. Ultimately, it is up to you to decide whether or not a given page is loading fast enough (i.e. performing well enough) given your available resources and the level of traffic the page receives.

Constructor injection vs method injection

Although I don't know enough about the innards of Laravel here to say for sure, I doubt there is a measurable difference in performance between injecting a repository in the constructor vs the method. However, the way there can be a difference is because any constructor-dependencies are always injected, where as method ones are only injected when the particular endpoint is called. So if you have a dependency injected in your constructor that is only used in one endpoint (or not at all), then that would be wasteful. If, however, a dependency is used in all the endpoints in the controller, I would just inject it in the constructor and be done with it. If you only have one method in your constructor, then it effectively shouldn't matter whether you inject in your constructor or method.

Injecting repositories in repositories

Whether or not that is a good idea or bad idea depends on the use case. There isn't an example of that here, so I would suggest another question to address that.


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.