Controller->Service->Repository->Model->all()
I have a construction in laravel(11). My code is as follows:
PostRepository.php
<?php
namespace AppRepositoriesEloquent;
use AppModelsPost;
use AppRepositoriesInterfacesIPost;
use IlluminateSupportCollection;
class PostRepository implements IPost
{
protected $model;
public function __construct(Post $model)
{
$this->model = $model;
}
public function all(): Collection
{
return $this->model->all();
}
public function find(int $id): ?Post
{
return $this->model->find($id);
}
//...etc
}
As you can see, this repo is dependent on the Post Model. If I want to change this model tomorrow, for example I want to add Redis etc., this will not be possible, and I don’t think it is in accordance with solid principles.
1- Do I need to create an interface for each Eloquent model?
2- Won’t binding them in AppserviceProvider break all these principles?
How do you solve this problem in your projects?