Do you actually have a use case where you need to call updateWith() on a User model?
Could you show us a use case? I really don't think you ever need to call 'updateWith' when you have a repository.
hummm I'm starting a new API. So I created this kind of update, but I really don't have a use case. What you're saying is that I probably only need a $this->userRepo->updateById(ID, [...]), don't you?
Um..it depends.
Could you post source code how you want to use it when you actually get your API?
You could just make a method in your repository.
updateWithValues(User $user, $values);
I will try to avoid to pass ID to a repository, except for finder methods (getById, find, etc) for a reason. The fact that you're using a repository means that you get an Entity back. When you update something, that means you queried an Entity from a repository. I don't find a good reason to pass an arbitrary ID along with values to a repository.
I am thinking for a while, if Repository is to decouple Controller from Model implementations, why we even return Eloquent Models and Collections from Repository functions.
halaei said:
I am thinking for a while, if Repository is to decouple Controller from Model implementations, why at all we even return Eloquent Models and Collections from Repository functions.
You can also abstract the implementation of models (UserInterface + EloquentUser + service provider for the models, inject them in the repository constructor) but this is a lot of work. The repository pattern have many advantages even without abstracting eloquent models, like testing and encapsulating persistance logic.
The repository pattern most people talk about in forum is not a traditional repository pattern (at least for the implantation.). Even with interfacing the Eloquent, you're still leaking the API of the Eloquent.
I believe that we're not looking for a perfect solution when we implement a repository with Eloquent. I think we use repository while knowing that it has its cons, but also pros. Like @pmall mentioned, it makes testing easier and let you have a finder-like methods across your whole application.
If you're looking for a repository that returns a POPO, google for repository pattern implementation with a data mapper pattern, not a repository with an activerecord. That will help.
Just a small thought - the correct constructor in Eloquent related models looks like
public function __construct($attributes = array(), $additionaParams) {
parent::__construct($attributes);
}
Maybe it can help, but I am not sure.
Hi, did you try to return and object from the getById($id) method that you can chain with the updateWithValues() method? If the returned object cannot chain the updateWithValues it will not have this method available when you use the following line:
$user = $this->userRepo->getById(ID)->updateWith([...]);
Do not return Eloquent Builder in the Repository methods. It is violationg Liskov Principle.
Eloquent Builder should be manipulated inside the Repository, then Controllers should manipulate only Repositories. Then Repositories will be the one manipulating Eloquents.
Maybe this will help, I use this implementation in my projects:
abstract class CommonEloquentAbstract implements
protected $model;
protected $queryBuilder;
private function setQueryBuilder()
{
if ($this->queryBuilder === NULL)
$this->queryBuilder = $this->model;
}
public function get()
{
$this->setQueryBuilder();
return $this->queryBuilder->get()->toArray();
}
public function paginate($limit = 15)
{
$this->setQueryBuilder();
return $this->queryBuilder->paginate($limit);
}
public function where($field, $operand=NULL, $value=NULL)
{
$this->setQueryBuilder();
$this->queryBuilder = $this->queryBuilder->where($field, $operand, $value);
return $this;
// return $this->model->where($field, $operand, $value);
}
public function orderBy($field, $order='ASC')
{
$this->setQueryBuilder();
$this->queryBuilder = $this->queryBuilder->orderBy($field, $order);
return $this;
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community