0

I'm trying to learn and implement the Repository Pattern in my app built with Laravel 5.6.

I have implemented my Controller:

class CompaniesController extends Controller
{
    protected $company;
    public function __construct(ICompanyRepository $company) {
        $this->company = $company;
    }
    public function index(){
       $companies = $this->company->getAllCompanies();
       return view('companies::index')->with("companies", $companies);
    }
}

Then I have implemented the repository interface:

interface ICompanyRepository
{
    public function getAllCompanies();
    public function findBy($att, $columns);
    public function getById($id);
    public function with($relations);
}

I have implemented My Repository:

class CompaniesRepository implements ICompanyRepository
{
    protected $model;
    public function __construct(Companies $model){
        $this->model = $model;
    }


    public function getAllCompanies(){
        return $this->model->all();
    }
    public function findBy($att, $columns)
    {
        return $this->model->where($att, $columns);
    }

    public function getById($id)
    {
        return $this->model->findOrFail($id);
    }


    public function with($relations)
    {
        return $this->model->with($relations);

    }
}

And then I have created the model:

class Companies extends Model
{
    protected $fillable = [];

    protected $casts = [
        'settings' => 'array'
    ];
    //my question is here!
    public function members(){
        return $this->hasMany('Companies\Entities\CompaniesMembers');
    }
}    

For now I have put the relations (in this case members function) in the model, but with this way, If I had to change my ORM, I should change both repository and model, because for now I use Eloquent, but I don't know if in future I will use Doctrine or others.

So my question is:

Where is the best place the relationships and the functions for db? Is right put all in the Model or It would be better put all in Repository?

3
  • i have a controller, entity, eloquentrepo, interface, model and in my eloquentrepo i combine them into a entity to object and in my view i use the entity functions like getName(). This will seperate all Commented Jul 26, 2018 at 13:26
  • thank you, could you post a small example of your implementation? Commented Jul 26, 2018 at 13:28
  • Yes wait a couple off minutes ;) Commented Jul 26, 2018 at 13:30

1 Answer 1

0

So in the EloquentRepo you make a function that combines company with companymembers by a foreach and use modelToObject($model) Something like this. I hope this will help you the good direction.

EloquentRepo:

private function modelToObject($model)
{
    if (is_null($model)) {
        $entity = null;
    } else {
        $entity = new Product(
            $model->{Model::COL_ID},
            $model->{Model::COL_NAME}
        );
    }

    return $entity;
}

Entity:

class Product{

    private $id;
    private $name;

    public function __construct(int $id, string $name) {
        $this->setId($id)
             ->setName($name);
    }

    public function setName(string $name): Product{
        $this->name = $name;

        return $this;
    }

    public function getName(): string {
        return $this->name;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but so, you don't use relationship? the function that combine 2 model is a DB::query? or is a relationshipt included in some model?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.