Download the PHP package iqbalatma/laravel-service-repo without Composer
On this page you can find all versions of the php package iqbalatma/laravel-service-repo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-service-repo
Laravel Service Repository
Laravel Service Repository is laravel package for service repository pattern. Service repository pattern helps you to separate concern and move logic into service class. From service class you can query all of your data from repository, not directly using model. This will help you to avoid redundant query and make it easier to maintain.
Upcomming
- [ ] Customize root of filter and order by config
- [ ] Publish service stub for customization
- [ ] Testing
- [ ] Type hint for predefined method on repository extend
How to install
Use composer to install Laravel Service Repository Package
Concept
Here is concept of service repository, you can see diagram below Controller will handle only http request and http response. This will make your controller thin. All of data from request will be validate on Validation class. All validation rule will be defined here After all data validated, data will be passed to service. In service, all of business logic works. Every business logic that need data or modify data from database will be communicate via repository. This repository already have some predefined query, but you are free to customize predefined query as you need.
Service
Service is use for your business logic. You can call service from controller or from another service. This package have base service and you can extend to this service. You can also generate service via artisan command.
This command will generate service for you in App/Services directory.
Repository
Use repository to query your database via model. So you do not need to write same query twice. You can generate repository on directroy App/Repositories via artisan command.
This command will generate repository for you in App/Repositories directory Here is example of UserRepository.
How to call repository from service
You can call method on repository via static or non-static method.
[!NOTE] Here is example call repository via static method
[!NOTE] Here is example call repository via non-static method
Predefined Method Query
You can call predefied method from service. Here is list of predefined method query and it's use case.
You can also chaining method before predefined method query.
[!NOTE] Here is how to use predefined method with chaining method for additional query
How to add predefined method
You can also create your own method on repository. For example you want to create query that will be execute in many place and want to avoid redundant code.
[!IMPORTANT] Create predefined method query with query prefix. So you can call statically without query prefix. Example : UserRepository::getAllActiveUser()
How to call model scope on repository
Sometimes you want to create model local scope and still want to use repository to call it.
you can call this scope TagRepository::active()->getAllDataPaginated();
How to use orderColumn method
Order column is predefined method that will use query param from http request for order requested data. Before use order column you need to defined column that allowed to order. You can define that orderable column inside the model.
As you can see, property $orderableColumns is array that has key and value. Key of this property is use to key from http request. You can customize this key into whatever you want The value of this property is mapping to column name of database table
To use this order you just need to call orderColumn method when calling repository
In other condition you can also pass parameter into orderColumn method to override orderable column
How to use filterColumn method
Filter column is predefined method to filter data via query param from http request Before use filterColumn method, you need to define list of column that allowed to filter
[!IMPORTANT] All of filter query using operator '=', but you can define custom operator based on your needs
To use filter column you just need to call and chain this method. You can also chain with other method like orderColumn
In other condition you can also pass parameter into filterColumn method to override filterable column
[!NOTE] You can also filter column on relation. But you need to define relation and filterable column on model
You when you want to filter relation of model, you need to make sure the relation is already defined. And then you need to create $relationFilterableColumns property. The key of this property is relation name and the value is filterable column just like $filterableColumns property.
[!NOTE] You can also override relation filterable column just pass array filterable column on second argumen of filterColumn. Example : filterColumn([], [ "profile" => [ "phone" => "profiles.pone" ] ])
How to custom operator on filterColumn
You just need to define array for column and operator