{"id":20531665,"url":"https://github.com/fernandozueet/service-laravel","last_synced_at":"2025-08-21T15:07:32.655Z","repository":{"id":56982098,"uuid":"181546303","full_name":"FernandoZueet/service-laravel","owner":"FernandoZueet","description":"Library that helps to separate the business rule part of the control. Leaving control leaner.","archived":false,"fork":false,"pushed_at":"2019-07-02T14:55:05.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-16T12:52:53.094Z","etag":null,"topics":["crud","repository","service","service-crud","service-laravel"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FernandoZueet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-15T18:44:45.000Z","updated_at":"2019-07-02T14:42:07.000Z","dependencies_parsed_at":"2022-08-21T12:20:12.680Z","dependency_job_id":null,"html_url":"https://github.com/FernandoZueet/service-laravel","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FernandoZueet%2Fservice-laravel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FernandoZueet%2Fservice-laravel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FernandoZueet%2Fservice-laravel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FernandoZueet%2Fservice-laravel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FernandoZueet","download_url":"https://codeload.github.com/FernandoZueet/service-laravel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242138791,"owners_count":20078007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crud","repository","service","service-crud","service-laravel"],"created_at":"2024-11-16T00:07:00.148Z","updated_at":"2025-03-06T02:44:25.023Z","avatar_url":"https://github.com/FernandoZueet.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Service laravel\n\nLibrary that helps to separate the business rule part of the control. Leaving control leaner.\n\n---\n\n## Requirements\n\n-   PHP 7.0 or newer;\n-   Laravel 5.5 or newer;\n\n---\n\n## Installation\n\n```bash\ncomposer require fernandozueet/service-laravel\n```\n\n---\n\n## Register the commands\n\nFile: \u003ccode\u003eapp/Console/Kernel.php\u003c/code\u003e\n\n```php\nprotected $commands = [\n    \\FzService\\Console\\ServiceCommand::class,\n    \\FzService\\Console\\ResourceCommand::class,\n];\n```\n\n---\n\n## Create a class of resource\n\nThe resource class is overwritten and gets a new customization feature from the fields that are returned.\n\nYou do not need to enter the resource prefix.\n\n```bash\nphp artisan fzservice:make:resource User\n```\n\nClass created:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Resources;\n\nuse FzService\\JsonResource;\n\nclass UserResource extends JsonResource\n{\n    /**\n     * Transform the resource into an array.\n     *\n     * @param  \\Illuminate\\Http\\Request  $request\n     * @return array\n     */\n    public function toArray($request)\n    {\n    \t$return = parent::toArray($request);\n\n    \treturn $this-\u003emountFields($return);\n    }\n}\n```\n\nHow to use the filter?\n\n```php\n\\App\\Resources\\UserResource::collection(\\App\\User::paginate(), ['fields' =\u003e 'user' =\u003e 'id,name']);\n```\n\n---\n\n## Create a class of service\n\nYou do not need to enter the service prefix.\n\n```bash\nphp artisan fzservice:make:service User\n```\n\nClass created:\n\n```php\n\u003c?php\n\nnamespace App\\Services;\n\nuse FzService\\Service;\nuse App\\Models\\User;\nuse App\\Http\\Resources\\UserResource;\n\nclass UserService extends Service\n{\n    /**\n    * Model class\n    *\n    * @var \\App\\Models\\User\n    */\n    protected $modelClass = User::class;\n\n    /**\n     * Read all rows\n     *\n     * @param array $params\n     * @param boolean $collection\n     * @return array|stdClass\n     */\n    public function readAll(array $params = [], bool $collection = true)\n    {\n    \treturn $this-\u003emountRead(function() use ($params) {\n\n            //\n\n        }, $params, $collection ? UserResource::class : null, []);\n    }\n\n}\n```\n\n---\n\n## Service Read\n\nMethod to assist in the assembly of queries eloquent.\n\n```php\n\u003c?php\n\nnamespace App\\Services;\n\nuse FzService\\Service;\nuse App\\Models\\User;\nuse App\\Http\\Resources\\UserResource;\n\nclass UserService extends Service\n{\n    /**\n    * Model class\n    *\n    * @var \\App\\Models\\User\n    */\n    protected $modelClass = User::class;\n\n    /**\n     * Read all rows\n     *\n     * @param array $params\n     * @param boolean $collection\n     * @return array|stdClass\n     */\n    public function readAll(array $params = [], bool $collection = true)\n    {\n    \treturn $this-\u003emountRead(function() use ($params) {\n\n            //wheres\n            if(!empty($params['id'])) {\n                $this-\u003emodelClass = $this-\u003emodelClass-\u003ewhere('id', $params['id']);\n            }\n            if(!empty($params['name'])) {\n                $this-\u003emodelClass = $this-\u003emodelClass-\u003ewhere('name', $params['name']);\n            }\n\n            //\n\n        }, $params, $collection ? UserResource::class : null, []);\n    }\n\n}\n```\n\nExample 1:\n\n\u003ccode\u003eSearching by name on page 1\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003ereadAll(['name' =\u003e \"User test\", 'page' =\u003e 1]);\n\n//Code equal to:\n$result = \\App\\Resources\\UserResource::collection(\\App\\User::where('name',$params['name'])-\u003epaginate());\n```\n\nExample 2:\n\n\u003ccode\u003eSearching by name without pagination\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003ereadAll(['name' =\u003e \"User test\"]);\n\n//Code equal to:\n$result = \\App\\Resources\\UserResource::collection(\\App\\User::where('name',$params['name'])-\u003eget());\n```\n\nExample 3:\n\n\u003ccode\u003eSearching by name and sorting by name and last_name ascending\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003ereadAll(['name' =\u003e \"User test\", 'sort' =\u003e 'name,last_name']);\n\n//Code equal to:\n$result = \\App\\Resources\\UserResource::collection(\\App\\User::where('name',$params['name'])-\u003eorderBy('name','ASC')-\u003eorderBy('last_name','ASC')-\u003eget());\n```\n\nExample 4:\n\n\u003ccode\u003eSearching by name and sorting by name downward\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003ereadAll(['name' =\u003e \"User test\", 'sort' =\u003e '-name']);\n\n//Code equal to:\n$result = \\App\\Resources\\UserResource::collection(\\App\\User::where('name',$params['name'])-\u003eorderBy('name','DESC')-\u003eget());\n```\n\nExample 5:\n\n\u003ccode\u003eSearching by name on page 1 and choosing the return fields\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003ereadAll(['name' =\u003e \"User test\", 'page' =\u003e 1, 'fields' =\u003e [ 'user' =\u003e 'id,name' ] ]);\n\n//Code equal to: (this option does not exist in the default laravel resource class)\n$result = \\App\\Resources\\UserResource::collection(\\App\\User::where('name',$params['name'])-\u003epaginate());\n```\n\nDisabling default methods:\n\nDefault methods: \u003ccode\u003epaginate and sort\u003c/code\u003e\n\n```php\npublic function readAll(array $params = [], bool $collection = true)\n{\n    return $this-\u003emountRead(function() use ($params) {\n\n        //\n\n    }, $params, $collection ? UserResource::class : null, ['paginate','sort']);\n}\n```\n\nDisabling Resource Class:\n\nTo disable the resource class simply enter the null value.\n\n```php\npublic function readAll(array $params = [], bool $collection = true)\n{\n    return $this-\u003emountRead(function() use ($params) {\n\n        //\n\n    }, $params, null, []);\n}\n```\n\n---\n\n## Service Insert\n\nMethod to insertion data eloquent.\n\n\u003ccode\u003einsert(array $values, array $exclude = [], array $include = [])\u003c/code\u003e\n\nor\n\n\u003ccode\u003ecreate(array $values, array $exclude = [], array $include = [])\u003c/code\u003e\n\nExample 1:\n\n\u003ccode\u003eInserting and returning an array with the entered data\u003c/code\u003e\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003einsert(['name' =\u003e \"User test\", \"last_name\" =\u003e 'other name']);\n\n//Insert through the Create method (declare fields to insert into the model)\n$result = $userService-\u003ecreate(['name' =\u003e \"User test\", \"last_name\" =\u003e 'other name']);\n\n//Code equal to:\n$model = new \\App\\User();\n$model-\u003ename = 'User test';\n$model-\u003elast_name = 'other name';\n$model-\u003esave();\n$result = $model-\u003etoArray();\n```\n\nExample 2:\n\n\u003ccode\u003e\nInserting and returning an array with the entered data.\n\nDeleting or adding a field from the returned data.\u003c/code\u003e\n\n```php\n//Service - exclude return fields\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003einsert(['name' =\u003e \"User test\", \"last_name\" =\u003e 'other name'], false, ['id','name']);\n\n//Service - visible return fields\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003einsert(['name' =\u003e \"User test\", \"last_name\" =\u003e 'other name'], false, [], ['id','name']);\n\n//Code equal to:\n$model = new \\App\\User();\n$model-\u003ename = 'User test';\n$model-\u003elast_name = 'other name';\n$model-\u003esave();\n$result = $model-\u003emakeHidden(['id','name'])-\u003etoArray(); //exclude return fields\n$result = $model-\u003emakeVisible(['id','name'])-\u003etoArray(); //visible return fields\n```\n\n---\n\n## Service Update\n\nMethod to update data eloquent.\n\n\u003ccode\u003eupdateById(int $id, array $values, array $exclude = [], array \\$include = [])\u003c/code\u003e\n\nExample:\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003eupdateById(1, ['name' =\u003e \"New name\"]);\n\n//Code equal to:\n$model = \\App\\User::where('id', 1);\n$model-\u003ename = 'New name';\n$model-\u003esave();\n$result = $model-\u003etoArray();\n```\n\n---\n\n## Service Delete\n\nMethod to delete data eloquent.\n\n\u003ccode\u003edeleteById(int $id)\u003c/code\u003e\n\nExample:\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003edeleteById(1);\n\n//Code equal to:\n$result = \\App\\User::where('id', 1)-\u003eforceDelete();\n```\n\n---\n\n## Service Soft Delete\n\nMethod to soft delete data eloquent.\n\n\u003ccode\u003esoftDeleteById(int $id)\u003c/code\u003e\n\nExample:\n\n```php\n//Service\n$userService = \\App\\Services\\UserService();\n$result = $userService-\u003esoftDeleteById(1);\n\n//Code equal to:\n$result = \\App\\User::where('id', 1)-\u003edelete();\n```\n\n---\n\n## Useful methods\n\nFile: \u003ccode\u003e\\FzService\\Service.php\u003c/code\u003e\n\n```php\n//Model\n$this-\u003emodelClass;\n\n//Create eloquent model instance\n$this-\u003enewQuery();\n\n//Set value field (array $params)\n$this-\u003esetValuesModel(['name' =\u003e 'User test']);\n\n//Save model (array $exclude = [], array $include = [], bool $returnArray = true)\n$this-\u003esaveModel();\n\n//Filter model (filtersModel(array $exclude = [], array $include = [], bool $returnArray = true))\n$this-\u003efiltersModel();\n```\n\n---\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/FernandoZueet/service-laravel/graphs/contributors) for details.\n\n## Security\n\nIf you discover security related issues, please email fernandozueet@hotmail.com instead of using the issue tracker.\n\n## Credits\n\n-   [Fernando Zueet](https://github.com/FernandoZueet)\n\n## License\n\nThe package is licensed under the MIT license. See [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffernandozueet%2Fservice-laravel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffernandozueet%2Fservice-laravel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffernandozueet%2Fservice-laravel/lists"}