Download the PHP package ttpn18121996/simple-repository without Composer
On this page you can find all versions of the php package ttpn18121996/simple-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ttpn18121996/simple-repository
More information about ttpn18121996/simple-repository
Files in ttpn18121996/simple-repository
Package simple-repository
Short Description Build repository and service patterns quickly and simply.
License MIT
Informations about the package simple-repository
Simple Repository
Content
- Installation
- Create repository
- Create service
- Use data filters for your query builder
- Use eloquent helper to customize query builder
- Customize the relationship builder
- Customize the query builder
- Set an authenticated user for the service
- Implementation and expansion
- ModelFactory attribute
- Use database processing functions safely
- Tips
Installation
Install using composer:
Next, publish SimpleRepository's resources using the simple-repository:install
command:
Create repository
Default Repository uses Eloquent, Run command make app/Repositories/Eloquent/UserRepository.php
file
and interface app/Repositories/Contracts/UserRepository.php
You can specify a model that your repository depends on during creation by adding options --model
or -m
.
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
After creating the repository remember to declare in
app/Providers/RepositoryServiceProvider.php
where protected $repositories
(By default they will be added automatically)
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/Repositories/Eloquent/ProvinceRepository.php
app/Repositories/Api/ProvinceRepository.php
Finally, what we need to do is change the binding between the abstract and the concrete in RepositoryServiceProvider
Create service
Run command for make a service. Ex: make app/Services/UserService.php
file.
You can specify the models your service depends on during creation by adding the --model or -m option.
You can use repositories instead of models. Specify the repositories your service depends on during creation by adding the --repo or -r option.
Use data filters for your query builder
Override the buildFilter
method in the class to customize the buildFilter
from the request.
Use eloquent helper to customize query builder
Get list with pagination in HasEloquentSupport
Customize the relationship builder
Similar to the filter builder, you can override the buildRelationships
method to customize relational query handling.
After overriding the buildRelationships
method,
the getPagination
method will include roles and permissions for each item.
Customize the query builder
If you want to customize the query, you can override the getBuilder
method.
If you use it in combination with HasFilter
, the order will be buildFilter
, buildRelationships
, getBuilder
Set an authenticated user for the service
Use authenticated users to use permission checks in the service.
Implementation and expansion
The simple repository provides two trait classes "HasFilter" and "Safetyable" that serve to build queries that handle sorting and filtering of data (HasFilter) and use transactions for data interaction (Safetyable). By default, the base service and base repository classes extend these two trait classes.
HasFilter provides a buildFilter
method. To use this feature, we need to pass the filters
parameter in the format:
Or you can use DTO as parameter for filter.
To create DTOs dynamically, you can use FilterAdapter
To fix the problem of 2 tables having the same field name or you want to change the field on the url to avoid revealing the table and field names in the database, the solution is to create a "transferredFields" property in your service class.
For example: users and roles tables both have a field of "name".
Or you can also directly override the "getTransferredField" method to transfer the field name.
ModelFactory attribute
When using model in service, it will look like this:
Instead of when using a service, dependent models will be initialized and injected automatically through the container service. Now, only when you call them are they initialized and stored. You can declare and use the model in the service through the ModelFactory attribute like this:
Use database processing functions safely
Instead of like this:
You can use the handleSafely() method instead. The first parameter is a callback for processing logic, the second parameter is the title of the logging. Let's say you get an exception, it will be of the form "Title: {message content}"
Services and repositories by default use the Safetyable trait. You can directly invoke the handleSafely() method within services/repositories.
Set up a log channel for the handleSafely() method to log when something goes wrong in the config/simple-repository.php
file
Tips
Inside the service class, you can call other services with the same namespace without importing them and instantiating
them. You can call them via the getService
method with the service name as the parameter value. For example,
App\Services\UserService
wants to use App\Services\RoleService
.
All versions of simple-repository with dependencies
ext-json Version *
illuminate/collections Version ^12.0
illuminate/console Version ^12.0
illuminate/contracts Version ^12.0
illuminate/database Version ^12.0
illuminate/support Version ^12.0
illuminate/log Version ^12.0