Answer the question
In order to leave comments, you need to log in
What is the point of passing data in this way?
use Corp\Portfolio;
class PortfoliosRepository extends Repository {
public function __construct(Portfolio $portfolio) {
$this->model = $portfolio;
}
$this->model=Portfolio::all();
Answer the question
In order to leave comments, you need to log in
I'm talking about the fact that the data is passed through the constructor. After all, you can simply define them in the code itself of the same fileIn the constructor in this case are passed to the data, and the object that will build queries. This is the same repository class - it encapsulates queries to the database, but it uses Eloquent models.
class PortfoliosRepository extends Repository {
public function __construct(Portfolio $portfolio) {
$this->model = $portfolio;
}
// Этот метод, скорее всего, есть в базовом классе
public function find(int $id): ?Portfolio
{
return $this->model->find($id);
}
// А это пример инкапсуляции сложной логики
public function featured(): Collection
{
return $this->model
->with(['some', 'relations'])
->where('field', 'value')
->orWhere('other_field', 'value')
->orderBy('created_at')
->limit(42)
->get()
->each
->append('mutated_attribute');
}
}
$repository = new PortfoliosRepository(
new Portfolio // <- не какое-то существующее портфолио из БД, а "пустой" объект
);
$repository->featured();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question