nathandunn / model-repositories
Easily create a repository-like entities using a Laravel Model
Installs: 5 117
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/database: ^10.0 || ^11.0
- illuminate/support: ^10.0 || ^11.0
README
Wraps around Eloquent models allowing them to be used as a repository-like entity.
Why?
Because getting a collection from a model "object" isn't really a models role, and is deceptive when injecting a model and using it in another context
Inspired by Jack Wagstaffe
Install
You can install the package with Composer by running the following command:
composer require nathandunn/model-repositories
Usage instructions
Below is an example of an example repository for a Record
model. This extends the base Repository
class and adds a custom forUser
method.
<?php namespace App\Records; use App\Users\User; use Illuminate\Database\Eloquent\Builder; use NathanDunn\Repositories\Repository; class RecordRepository extends Repository { /** * @param Record $record */ public function __construct(Record $record) { parent::__construct($record); } public function forUser(User $user): Builder { return $this->model->where('user_id', '=', $uuid); } }
You can inject the RecordRepository
in a controller:
<?php namespace App\Http\Controllers; use App\Http\Resources\RecordResource; use App\Records\RecordRepository; use Illuminate\Http\Request; class RecordsController extends Controller { protected RecordRepository $recordRepository; public function __construct(RecordRepository $recordRepository) { $this->recordRepository = $recordRepository; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $user = $request->user(); $records = $this->recordRepository->getForUser($user); return RecordResource::collection($records); } }