New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

nathandunn/model-repositories

Easily create a repository-like entities using a Laravel Model
26,415

Larablocks Analysis

nathandunn/model-repositories works with Laravel 10 to 13 (latest release v1.3.1, Apr 11, 2026).

Recommendation checks

Solid choice

Solid choice: maintained and up to date, meets 2 of 4 checks; below the bar for Recommended on use.

  • Works with Laravel 13, the current version
  • Released in the last 12 months (Apr 11, 2026)
  • 3,240 downloads a month (needs 10,000)
  • 0 GitHub stars (needs 100)

State

Maintained
  • Last stable release: Apr 11, 2026.
  • 1 release in the last 12 months.
  • First released: Feb 18, 2023.
  • Monthly downloads: 3,240.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 30, 2026.

Install
composer require nathandunn/model-repositories
Latest Version:v1.3.1
PHP:^8.1
Laravel:10 – 13
Last Updated:Apr 11, 2026
Links: GitHub  ·  Packagist
Maintainer: nathandunn

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:

composer require nathandunn/model-repositories

📦 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() or getForXyz().

🧠 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:

$records = Record::where('user_id', $user->id)->paginate();

You can use:

$records = $recordRepository->getPaginatedForUser($user);

🛠 Usage

1. Create a Custom Repository

<?php

namespace App\Records;

use App\Users\User;
use Illuminate\Database\Eloquent\Builder;
use NathanDunn\Repositories\Repository;

class RecordRepository extends Repository
{
    public function __construct(Record $record)
    {
        parent::__construct($record);
    }

    public function forUser(User $user): Builder
    {
        return $this->model->where('user_id', '=', $user->id);
    }
}

2. Inject into Your Controller

<?php

namespace App\Http\Controllers;

use App\Http\Resources\RecordResource;
use App\Records\RecordRepository;
use Illuminate\Http\Request;

class RecordsController extends Controller
{
    public function __construct(
        protected RecordRepository $recordRepository
    ) {}

    public function index(Request $request)
    {
        $records = $this->recordRepository->getPaginatedForUser($request->user());

        return RecordResource::collection($records);
    }
}

🔁 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:

  1. Call forUser($user) (your custom method)
  2. 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:

$repo->getForUser($user);              // ->forUser()->get()
$repo->getPaginatedForUser($user);     // ->forUser()->paginate()
$repo->getCountForUser($user);         // ->forUser()->count()

✅ Requirements

  • Laravel 10+
  • PHP 8.1+

📚 License

MIT © Nathan Dunn

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

431,187,642 4,660

Version History

Version Released PHP Laravel License
v1.3.1 ^8.1 ^10.0 || ^11.0 || ^12.0 || ^13.0
v1.3.0 ^8.1 ^10.0 || ^11.0 || ^12.0
v1.2.0 ^8.1 ^10.0 || ^11.0 || ^12.0
v1.1.0 ^8.1 ^10.0 || ^11.0
1.0.0 ^8.1 ^10.0