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

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

ttpn18121996/simple-repository
==============================

Build repository and service patterns quickly and simply.

v5.0.0(1mo ago)0253[1 PRs](https://github.com/ttpn18121996/simple-repository/pulls)MITPHPPHP ^8.2

Since Oct 2Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/ttpn18121996/simple-repository)[ Packagist](https://packagist.org/packages/ttpn18121996/simple-repository)[ RSS](/packages/ttpn18121996-simple-repository/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (47)Used By (0)

Simple Repository
=================

[](#simple-repository)

[`PHP v8.2`](https://php.net)

[`Laravel v11.x`](https://github.com/laravel/laravel)

Content
-------

[](#content)

- [Installation](#installation)
- [Create repository](#create-repository)
- [Create service](#create-service)
- [Use data filters for your query builder](#use-data-filters-for-your-query-builder)
- [Use eloquent helper to customize query builder](#use-eloquent-helper-to-customize-query-builder)
    - [Customize the relationship builder](#customize-the-relationship-builder)
    - [Customize the query builder](#customize-the-query-builder)
- [Set an authenticated user for the service](#set-an-authenticated-user-for-the-service)
- [Implementation and expansion](#implementation-and-expansion)
- [ModelFactory attribute](#modelfactory-attribute)
- [Use database processing functions safely](#use-database-processing-functions-safely)
- [Tips](#tips)

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

[](#installation)

Install using composer:

```
composer require ttpn18121996/simple-repository
```

Next, publish SimpleRepository's resources using the `simple-repository:install` command:

```
php artisan simple-repository:install
```

Create repository
-----------------

[](#create-repository)

Default Repository uses Eloquent, Run command make `app/Repositories/Eloquent/UserRepository.php` file and interface `app/Repositories/Contracts/UserRepository.php`

```
php artisan make:repository UserRepository
```

You can specify a model that your repository depends on during creation by adding options `--model` or `-m`.

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

#OR

php artisan make:repository UserRepository -m User
```

Use another repository during the build by adding the `--repo` or `-r` option. For example, if you want to use an external service instead of Eloquent, now the repository will be created in the path `app/Repositories/Api/UserRepository.php`

```
php artisan make:repository UserRepository --repo Api

#OR

php artisan make:repository UserRepository -r Api
```

After creating the repository remember to declare in `app/Providers/RepositoryServiceProvider.php` where `protected $repositories`(By default they will be added automatically)

```
protected $repositories = [
    ...
    \App\Repositories\Contracts\UserRepository::class => \App\Repositories\Eloquent\UserRepository::class,
]
```

The example shows the dynamic extension of the repository pattern. We use province data in the database. After a while, we realize that using local data is no longer suitable and we want to use a data source from an external web service. Editing the existing `Eloquent\ProvinceRepository` content will result in errors or be difficult to revert to before. Instead, we will create a new repository called `Api\ProvinceRepository` while still ensuring its accuracy like the old repository.

```
/app
├---/Providers
|   ├---RepositoryServiceProvider.php
├---/Repositories
|   ├---/Api
|   |   ├---ProvinceRepository.php
|   ├---/Contracts
|   |   ├---ProvinceRepository.php
|   ├---/Eloquent
|   |   ├---ProvinceRepository.php

```

app/Repositories/Eloquent/ProvinceRepository.php

```
