PHPackages                             saineshmamgain/laravel-repositories - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. saineshmamgain/laravel-repositories

ActiveProject[Utility &amp; Helpers](/categories/utility)

saineshmamgain/laravel-repositories
===================================

A package to create repositories in your laravel applications.

1.0.8(4y ago)21492MITPHP

Since Jun 21Pushed 4y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (3)Versions (11)Used By (0)

Laravel Repositories
====================

[](#laravel-repositories)

Laravel Repositories is a Laravel package that makes creating and managing repositories a breeze.

[![GitHub release (latest by date)](https://camo.githubusercontent.com/6b34e4d96ad5f246d7b18a3d3719e5d9753f64120cf2be8405f7ef432ee034ee/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965733f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/6b34e4d96ad5f246d7b18a3d3719e5d9753f64120cf2be8405f7ef432ee034ee/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965733f7374796c653d666c61742d737175617265)[![Latest Stable Version](https://camo.githubusercontent.com/fa5d47391695ce0b92571ae045540e06a4133e9d40c92391d44d0041373230fd/687474703a2f2f706f7365722e707567782e6f72672f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965732f76)](https://packagist.org/packages/saineshmamgain/laravel-repositories) [![Total Downloads](https://camo.githubusercontent.com/b68c99356d5f720a88a7c310b0294c13372c26a122e883de2a936d27e239c830/687474703a2f2f706f7365722e707567782e6f72672f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965732f646f776e6c6f616473)](https://packagist.org/packages/saineshmamgain/laravel-repositories) [![Latest Unstable Version](https://camo.githubusercontent.com/fc3ca103e5b6d3844887da96fa6aa55815b6d332fafa8e890d539507319c1fd1/687474703a2f2f706f7365722e707567782e6f72672f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965732f762f756e737461626c65)](https://packagist.org/packages/saineshmamgain/laravel-repositories) [![License](https://camo.githubusercontent.com/4e25cd167f530666dc844245a7a5dd8715220be7ddbe97f1e794ef966bac5c61/687474703a2f2f706f7365722e707567782e6f72672f7361696e6573686d616d6761696e2f6c61726176656c2d7265706f7369746f726965732f6c6963656e7365)](https://packagist.org/packages/saineshmamgain/laravel-repositories)

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

[](#installation)

Use the package manager [composer](https://getcomposer.org/download/) to install the package.

```
composer require saineshmamgain/laravel-repositories
```

Usage
-----

[](#usage)

### Create a repository

[](#create-a-repository)

```
#php artisan make:repository {ModelName}

php artisan make:repository User
```

This command will create a `UserRepository` in `App\Repositories` namespace.

### Inserting a record

[](#inserting-a-record)

```
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UsersController extends Controller{

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required'
            'password' => 'required'
        ]);

        $validated = $validator->validated();

        UserRepository::init()
            ->create($validated);

        return redirect()->back();
    }
}
```

### Updating a record

[](#updating-a-record)

```
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;

class UsersController extends Controller{

    public function update(Request $request, User $user)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required'
            'password' => 'required'
        ]);

        $validated = $validator->validated();

        UserRepository::init($user)
            ->update($validated);

        return redirect()->back();
    }
}
```

### Deleting a record

[](#deleting-a-record)

This package also supports `softDelete`. By default the package will check if the model uses `softDelete` if yes it will `softDelete` the model. To delete a model permanently just pass `true` while calling `destroy(true)` method.

```
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;

class UsersController extends Controller{

    public function destroy(Request $request, User $user)
    {
        UserRepository::init($user)
            ->destroy();

        return redirect()->back();
    }
}
```

### Querying from model

[](#querying-from-model)

Laravel already provides a nice abstraction for writing queries so querying with using repository can be optional. You can just use models to write queries. But if you still want to use the repository for querying then the repository provides a `query()` method that proxies model. After calling `query()` method you can chain all the methods provided by `Eloquent`.

```
// In UsersController
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;

class UsersController extends Controller{

    public function index(Request $request)
    {
        $users = UserRepository::init()
            ->query()
            ->where('status', '=', 1)
            ->paginate();

        return view('users.index')->with(compact('users'));
    }
}
```

### Repository Hooks

[](#repository-hooks)

This package provides some hooks for the `create`, `update`, `destroy` and `restore` methods.

**List of hooks are:**

1. For `create` method `beforeCreate` and `afterCreate`.
2. For `update` method `beforeUpdate` and `afterUpdate`.
3. For `destroy` method `beforeDestroy`.
4. For `restore` method `afterRestore`.
5. Additional `beforeSave` and `afterSave` hooks which work on both `create` and `update` methods.

**Usage of Hooks**

Let's take a scenario for where you want to hash a users password while creating or updating.

```
// In App\Repositories\UserRepository create a method beforSave

protected function beforeSave($fields)
{
    if(array_key_exists('password', $fields)){
        $fields['password'] = Hash::make($fields['password']);
    }
    return $fields;
}
```

That's it! Now for every create and update action the password field will be hashed. And yes don't forget to check if `password` key exists in the `$fields` array.

`$fields` is the array of fields that were passed while calling `create` or `update` method.

Now let's take another slightly complicated example:

Suppose you are submitting a form while creating a user that also has a field called `roles`. Since `users` table doesn't have any roles column then while calling `create` method Repository will throw an exception.

You can tackle this problem using hooks.

```
// In App\Repositories\UserRepository create a method beforSave

protected function beforeSave($fields)
{
    if(array_key_exists('roles', $fields)){
        unset($fields['roles']);
    }
    return $fields;
}

protected function afterSave($orignal_fields, $fields)
{
    if(array_key_exists('roles', $orignal_fields)){
        $this->model->roles()->sync($orignal_fields['roles']);
    }
    return $this->model;
}
```

All three methods `afterSave`, `afterCreate` and `afterUpdate` will receive two parameters `$original_fields`, those were submitted originally and `$fields`, those were returned using `before` hooks. So you can safely unset all the fields that are not needed while creating/updating a record and use them after the creating/updating the record.

**List of Hooks and their Return values**

```
protected function beforeCreate(array $fields)
{
    return $fields;
}

protected function afterCreate(array $original_fields, array $fields)
{
    return $this->model;
}

protected function beforeUpdate(array $fields)
{
    return $fields;
}

protected function afterUpdate(array $original_fields, array $fields)
{
    return $this->model;
}

protected function beforeSave(array $fields)
{
    return $fields;
}

protected function afterSave(array $original_fields, array $fields)
{
    return $this->model;
}

protected function beforeDestroy(bool $isSoftDeletable, bool $permanent)
{
    return $this->model;
}

protected function afterRestore()
{
    return $this->model;
}
```

What's Next
-----------

[](#whats-next)

1. Covering More Tests

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 test before submitting a PR.

License
-------

[](#license)

[MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

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 ~16 days

Recently: every ~30 days

Total

9

Last Release

1710d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f0b25c15d5d4ececbd13df68dde1877a9767cb4216f7df12c06ade2aed7f286c?d=identicon)[saineshmamgain](/maintainers/saineshmamgain)

---

Top Contributors

[![saineshmamgain](https://avatars.githubusercontent.com/u/16660523?v=4)](https://github.com/saineshmamgain "saineshmamgain (31 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/saineshmamgain-laravel-repositories/health.svg)

```
[![Health](https://phpackages.com/badges/saineshmamgain-laravel-repositories/health.svg)](https://phpackages.com/packages/saineshmamgain-laravel-repositories)
```

PHPackages © 2026

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