Created
August 7, 2021 06:57
-
-
Save JustSteveKing/4cd893436c7629993266921fb24e3da4 to your computer and use it in GitHub Desktop.
An example on Laravel Eloquent Repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Repository | |
{ | |
protected Builder $query; | |
public function all(): null|Collection | |
{ | |
return $this->query->get(); | |
} | |
} | |
class UserRepository extends Repository | |
{ | |
public function __construct(string $model) | |
{ | |
$this->query = $model->query(); | |
} | |
} |
So for every model, I will come here to resister the repository ?
Controller:
class UserController extends Controller
{
public function __construct(
protected UserRepository $repository,
) { }
public function index(): Response
{
return $this->repository->all();
}
}
So for every model, I will come here to resister the repository ?
Yep! That's the best way to do it
oh well, Let me get to work.
Thank you very much Steve.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Service Provider: