Download the PHP package nathandunn/model-repositories without Composer
On this page you can find all versions of the php package nathandunn/model-repositories. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nathandunn/model-repositories
More information about nathandunn/model-repositories
Files in nathandunn/model-repositories
Package model-repositories
Short Description Easily create a repository-like entities using a Laravel Model
License
Informations about the package model-repositories
Laravel Model Repositories
A lightweight abstraction for Laravel Eloquent models that enables cleaner, more expressive repository-style access to your application's data layer.
"Fetching collections or business-specific queries from models violates separation of concerns. This package provides an elegant way to wrap Eloquent models into repository-like entities."
Inspired by Jack Wagstaffe
๐ Installation
Install via Composer:
๐ฆ What This Provides
This package allows you to:
- Encapsulate complex query logic outside your Eloquent models.
- Maintain proper separation between models and querying logic.
- Leverage dynamic method chaining like
getPaginatedForUser()
orgetForXyz()
.
๐ง Concept
Instead of injecting Eloquent models directly into services or controllers, create a repository class per model that encapsulates query logic.
For example, instead of:
You can use:
๐ Usage
1. Create a Custom Repository
2. Inject into Your Controller
๐ Dynamic Method Chaining
The base Repository
class supports auto-chaining based on naming convention. The following helper prefixes are recognised:
Prefix | Description |
---|---|
get |
Fetch a collection |
getPaginated |
Fetch and paginate |
getCount |
Get count of query results |
first / firstOrFail |
Fetch a single item |
exists |
Check for existence |
So calling getPaginatedForUser($user)
will:
- Call
forUser($user)
(your custom method) - Then call
paginate()
on the result
This is handled by __call()
in the base class using a naming convention-based resolver.
๐งช Example Call Chain
Given a repository method forUser(User $user): Builder
, these dynamic calls become possible:
โ Requirements
- Laravel 10+
- PHP 8.1+
๐ License
MIT ยฉ Nathan Dunn
All versions of model-repositories with dependencies
illuminate/database Version ^10.0 || ^11.0 || ^12.0
illuminate/support Version ^10.0 || ^11.0 || ^12.0