Coder Social home page Coder Social logo

Comments (3)

Firman95 avatar Firman95 commented on August 31, 2024 1

@freevital
If I inject BRepo in ARepo, how to inject ARepo in BRepo?

This is my BRepo

class BRepository extends BaseRepository
{
    /**
     * @var ARepository
     */
    private $aRepo;

    /**
     * @param Application $app
     * @param ARepository $aRepo
     */
    public function __construct(Application $app, ARepository $aRepo)
    {
        parent::__construct($app);

        $this->aRepo = $aRepo;
    }
}

This is my ARepo

class ARepository extends BaseRepository
{
    /**
     * @var BRepository
     */
    private $bRepo;

    /**
     * @param Application $app
     * @param BRepository $bRepo
     */
    public function __construct(Application $app, BRepository $bRepo)
    {
        parent::__construct($app);
        $this->bRepo = $bRepo;
    }
}

I get Http Error 500, "domain.com is currently unable to handle this request", for all routing that use ARepository or BRepository class. I can inject ARepository with all other repositories with no problem except for BRepository class. Thanks in advance.

from laravel-repository.

Firman95 avatar Firman95 commented on August 31, 2024

Solved. This is how i declare repository in repository $this->aRepo = \App::make(ARepository::class);

class BRepository extends BaseRepository {

  protected $aRepo;

  public function model()
  {
      return "App\Models\B";
  }

  public function createNewB(){
    $this->aRepo = \App::make(ARepository::class);
    DB::beginTransaction();
    try{
      $this->aRepo->createNewA();
      $this->model->create([
        'column_b' => 'test'
      ]);
      DB::commit();
    }catch(Exception $e){
      DB::rollback();
    }
  }
}

from laravel-repository.

freevital avatar freevital commented on August 31, 2024

You may inject another repository in construct method:

class BRepository extends BaseRepository
{
    /**
     * @var ARepository
     */
    private $aRepo;

    /**
     * @param Application $app
     * @param ARepository $aRepo
     */
    public function __construct(Application $app, ARepository $aRepo)
    {
        parent::__construct($app);

        $this->aRepo = $aRepo;
    }
}

from laravel-repository.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.