A repository pattern example for Laravel to switch between eloquent and query builder.
- Define repository classes and interfaces in
app\Repositories
directory. - Define a constructor in all controllers:
public function __construct(StrategyContext $strategy)
{
parent::__construct($strategy);
$this->strategy->repository(ArticleRepositoryInterface::class);
}
- Call method of the repository like the following example:
$querybuilderData = $this->strategy->querybuilder()->list();
OR
$eloquentData = $this->strategy->eloquent()->list();