PHPackages                             touhidurabir/laravel-model-repository - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. touhidurabir/laravel-model-repository

ActiveLibrary

touhidurabir/laravel-model-repository
=====================================

A package to implement repository pattern for laravel models

1.1.3(4y ago)321.7k3MITPHPPHP &gt;=7.2.0

Since Aug 21Pushed 4y ago2 watchersCompare

[ Source](https://github.com/touhidurabir/laravel-model-repository)[ Packagist](https://packagist.org/packages/touhidurabir/laravel-model-repository)[ RSS](/packages/touhidurabir-laravel-model-repository/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (6)Versions (8)Used By (0)

Laravel Model Repository
========================

[](#laravel-model-repository)

A simple package to use **Repository Pattern** approach for laravel models .

Repository pattern
------------------

[](#repository-pattern)

Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer. [Microsoft](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design)

Installation
------------

[](#installation)

Require the package using composer:

```
composer require touhidurabir/laravel-model-repository
```

To publish the config file:

```
php artisan vendor:publish --provider="Touhidurabir\ModelRepository\ModelRepositoryServiceProvider" --tag=config
```

Command and Configuration
-------------------------

[](#command-and-configuration)

To use this package, you need to have repository class bound to laravel model class . This package includes a command that make it easy to to create repository classes from command line . to create a new repository class, run the following command

```
php artisan make:repository UserRepository --model=User
```

The above command will create a new repository **UserRepository** class in **App\\Repositories** path . the **--model** option to define which laravel model class to target for this repositoty class . The content of **UserRepository** will look like

```
namespace App\Repositories;

use Touhidurabir\ModelRepository\BaseRepository;
use App\Models\User;

class UserRepository extends BaseRepository {

	/**
     * Constructor to bind model to repo
     *
     * @param  object $user
     * @return void
     */
    public function __construct(User $user) {

        $this->model = $user;

        $this->modelClass = get_class($user);
    }

}
```

This package by default assume all **models** are located in path **App\\Models** and use the path **App\\Repositories** to store the **repository** classes. But also possible to provide custom repositories class path and different model class path . for example

```
php artisan make:repository App\\SomeOtherPath\\UserRepository --model=App\\OtherModelPath\\User
```

The above command will try to store the repository class to path **App\\SomeOtherPath** and will create a directory named **SomeOtherPath** if not already exists. Will also try to resolve model path/namespace from **App\\OtherModelPath** .

Check the **config** file after publishing at the **config/model-repository.php** to see the default settings configurations .

It is also possible to replace an existing repository file via the command when passing the flag **--replace**

```
php artisan make:repository UserRepository --model=User --replace
```

The above command will replace the already exiting **UserRepository.php** at the given path with newly generated one.

> Be ery carefull of the replacing ability as if your repository class contains any custom code, that will be fully replaced with a newly generated file and those custom changes will be lost.

Usage
-----

[](#usage)

The best way to use the repository classes via **Dependency Injection** through the **controller** classes . for example :

```
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;

class UserController extends Controller {

    /**
     * The resource repository instance
     *
     * @var mixed
     */
    protected $userRepository;

	/**
     * create a new controller instance
     *
     * @param  \App\Repositories\UserRepository         $userRepository
     * @return void
     */
    public function __construct(UserRepository $userRepository) {

        $this->userRepository = $userRepository;
    }
}
```

And in that way one can already get a fully qualified user repository class . Also to manually initiated :

```
namespace App\Http\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
...

$userRepository = new UserRepository(new User);
```

Or through static constructor

```
$userRepository = UserRepository::withModel(new User);
```

The repository class will have following features/abilities .

### Create

[](#create)

To create a new model record, just call the **create** method on repositoty class and pass the data attributes as :

```
$this->userRepository->create([
    ...
]);
```

### Update

[](#update)

To update a existing model record, call the **update** method of the repository class . the **update** method will require 2 params , the data attributes and the model redored primary key value or an exiting model instance .

To update with primary key for user with primary key of id with value 10

```
$primaryKeyValue = 10;

$this->userRepository->update([
    ...
], $primaryKeyValue);
```

or To update the already retrived model record :

```
$user; // the already retrived model record instance

$this->userRepository->update([
    ...
], $user);
```

### Find

[](#find)

To find a model record, use the **find** method of the repository class

```
$this->userRepository->find(1); // find the id(primary key) of 1
$this->userRepository->find([1,2,3]); // find the id(primary key) of 1,2 and 3
```

The **find** method can also work with array where it will use those as **AND WHERE** query and return the first record that match

```
$this->userRepository->find(['email' => 'somemail@mail.test']);
```

By passing the optional relations array as the second argument to **find** method will load the relations along with model record

```
$this->userRepository->find(1, ['profile']); // find the id(primary key) of 1
$this->userRepository->find([1,2,3], ['profile']); // find the id(primary key) of 1,2 and 3
```

The thrid agument is a optional boolen which is by default set to **false** . By setting it to **true**, it will thorw the **\\Illuminate\\Database\\Eloquent\\ModelNotFoundException** when a model record not found .

```
$this->userRepository->find(1, ['profile'],  true); // find the id(primary key) of 1
$this->userRepository->find([1,2,3], [], true); // find the id(primary key) of 1,2 and 3
```

### All Records

[](#all-records)

To get back all records, use the **all** method of repository class

```
$this->userRepository->all();
```

### Delete

[](#delete)

To Delete a model record, use the **delete** method of repository class

```
$this->userRepository->delete(1);
```

The **delete** method can wrok with model instance or the same kind of argument passed to the repository class **find** method .

```
$this->userRepository->delete($user); // delete the alredt retrived $user model instance
$this->userRepository->delete(1); // delete user id of 1
$this->userRepository->delete([1,2,3]); // delete user id of 1,2 and 3
$this->userRepository->delete(['email' => 'somemail@mail.test']); // delete user with email of somemail@mail.test
```

The **delete** method also check for the **SoftDelete** feature , that is if the model is using the **Illuminate\\Database\\Eloquent\\SoftDeletes** trait, the it will do the soft delete of given model records.

### Force Delete

[](#force-delete)

To Force Delete a model record, use the **forceDelete** method of repository class

```
$this->userRepository->forceDelete(1);
```

The **delete** method can wrok with model instance or the same kind of argument passed to the repository class **find** method .

```
$this->userRepository->forceDelete($user); // delete the alredt retrived $user model instance
$this->userRepository->forceDelete(1); // delete user id of 1
$this->userRepository->forceDelete([1,2,3]); // delete user id of 1,2 and 3
$this->userRepository->forceDelete(['email' => 'somemail@mail.test']); // delete user with email of somemail@mail.test
```

The **delete** method also check for the **SoftDelete** feature, that is regardless of the model is using the **Illuminate\\Database\\Eloquent\\SoftDeletes** trait, the it will remove those records from DB.

### Restore

[](#restore)

To Restore a model record that has soft deleted, use the **forceDelete** method of repository class

```
$this->userRepository->restore(1);
```

The **restore** will only works for those models that use the **SoftDeletes** feature . It try to use the restore on the model that do not have **SoftDeletes** implemented, it will throw an exception.

The **restore** method can wrok with model instance or array of model primary keys .

```
$this->userRepository->restore($user); // restore the already retrived $user model instance
$this->userRepository->restore(1); // restore user id of 1
$this->userRepository->restore([1,2,3]); // restore user id of 1,2 and 3test
```

Other Features
--------------

[](#other-features)

### Get Model

[](#get-model)

As this package does not handle all of the features of Eloquent and if any other Eloquent method need to use to build complex query, we need the model instance . to get the model instance

```
$this->userRepository->getModel();
```

Also to set/update the model later

```
$this->userRepository->setModel(new User);
$this->userRepository->setModel($user);
```

### Model Sanitizer

[](#model-sanitizer)

The BaseRepository class includes a model sanitizer that will automatically sanitize passed array attributes on model record create/update . Here sanatize means it will remove any element from the data array to match with the model table schema while at the same time respecting model **$fillable** and **$hidden** properties .

The implementation of these methods are as such

```
/**
 * Sanitize data list to model fillables
 *
 * @param  array   $data
 * @return array
 */
public function sanitizeToModelFillable(array $data) {

    $classModel   = $this->model->getModel();
    $fillable     = $classModel->getFillable();

    $fillables = ! empty($fillable)
                    ? $fillable
                    : array_diff(
                        array_diff(
                            Schema::getColumnListing($classModel->getTable()),
                            $classModel->getGuarded()
                        ),
                        $classModel->getHidden()
                    );

    return array_intersect_key($data, array_flip($fillables));
}
```

So even if extra details passed, it will be ignored or some columns passed that in the **$fillable** or **$hidden** list.

```
$user = $this->userRepository->create([
    'name' => 'User Name',
    'email' => 'somemail@mail.test',
    'password' => Hash::make('password'),
    'date_of_birth' => '1990-12-08' // This date_of_birth column not present in users table
]);
```

The above code will run without any issue while a simple model create method will throw exception .

```
$user = $this->userRepository->create($request->validated());

$profile = $this->profileRepository->create($request->validated());
```

This become very useful when in one single controller method do need to push data to multiple model table

Contributing
------------

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License
-------

[](#license)

[MIT](./LICENSE.md)

###  Health Score

31

—

LowBetter than 71% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

7

Last Release

1607d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c6009d764c48df4a55a8645c349f90a99c7e6e694b06c60b580e1da278d1db5?d=identicon)[touhidurabir](/maintainers/touhidurabir)

---

Top Contributors

[![touhidurabir](https://avatars.githubusercontent.com/u/2587979?v=4)](https://github.com/touhidurabir "touhidurabir (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/touhidurabir-laravel-model-repository/health.svg)

```
[![Health](https://phpackages.com/badges/touhidurabir-laravel-model-repository/health.svg)](https://phpackages.com/packages/touhidurabir-laravel-model-repository)
```

###  Alternatives

[microweber/microweber

New generation CMS with drag and drop

3.4k13.8k](/packages/microweber-microweber)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
