Here's a quick tip. If you have embraced the repository pattern for your Laravel application, you may have run into a 502 bad gateway
. This can happen if you choose to setup the constructors of one or more repositories to mutually direct inject each other. The result is a loop or recursion of the dependencies between the objects being direct injected.
class CurrentRepository
{
protected $others;
public function __construct(OtherRepository $others)
{
$this->others = $others;
}
}
If you have pulled out your hair with this before, here's the tip. Instead, you can app()
or app()->make()
the repositories you need access to within a protected function and avoid the injection loop altogether.
// Now you can access the other repository within
// the current repository with $this->others()
protected function others()
{
return app(OtherRepository::class);
}
Please note, if your object dependencies are looping, you may want to restructure your code so that the repositories stop depending on each other altogether.