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?