Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fernandozueet/service-laravel

Library that helps to separate the business rule part of the control. Leaving control leaner.
https://github.com/fernandozueet/service-laravel

crud repository service service-crud service-laravel

Last synced: about 3 hours ago
JSON representation

Library that helps to separate the business rule part of the control. Leaving control leaner.

Awesome Lists containing this project

README

        

# Service laravel

Library that helps to separate the business rule part of the control. Leaving control leaner.

---

## Requirements

- PHP 7.0 or newer;
- Laravel 5.5 or newer;

---

## Installation

```bash
composer require fernandozueet/service-laravel
```

---

## Register the commands

File: app/Console/Kernel.php

```php
protected $commands = [
\FzService\Console\ServiceCommand::class,
\FzService\Console\ResourceCommand::class,
];
```

---

## Create a class of resource

The resource class is overwritten and gets a new customization feature from the fields that are returned.

You do not need to enter the resource prefix.

```bash
php artisan fzservice:make:resource User
```

Class created:

```php
mountFields($return);
}
}
```

How to use the filter?

```php
\App\Resources\UserResource::collection(\App\User::paginate(), ['fields' => 'user' => 'id,name']);
```

---

## Create a class of service

You do not need to enter the service prefix.

```bash
php artisan fzservice:make:service User
```

Class created:

```php
mountRead(function() use ($params) {

//

}, $params, $collection ? UserResource::class : null, []);
}

}
```

---

## Service Read

Method to assist in the assembly of queries eloquent.

```php
mountRead(function() use ($params) {

//wheres
if(!empty($params['id'])) {
$this->modelClass = $this->modelClass->where('id', $params['id']);
}
if(!empty($params['name'])) {
$this->modelClass = $this->modelClass->where('name', $params['name']);
}

//

}, $params, $collection ? UserResource::class : null, []);
}

}
```

Example 1:

Searching by name on page 1

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->readAll(['name' => "User test", 'page' => 1]);

//Code equal to:
$result = \App\Resources\UserResource::collection(\App\User::where('name',$params['name'])->paginate());
```

Example 2:

Searching by name without pagination

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->readAll(['name' => "User test"]);

//Code equal to:
$result = \App\Resources\UserResource::collection(\App\User::where('name',$params['name'])->get());
```

Example 3:

Searching by name and sorting by name and last_name ascending

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->readAll(['name' => "User test", 'sort' => 'name,last_name']);

//Code equal to:
$result = \App\Resources\UserResource::collection(\App\User::where('name',$params['name'])->orderBy('name','ASC')->orderBy('last_name','ASC')->get());
```

Example 4:

Searching by name and sorting by name downward

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->readAll(['name' => "User test", 'sort' => '-name']);

//Code equal to:
$result = \App\Resources\UserResource::collection(\App\User::where('name',$params['name'])->orderBy('name','DESC')->get());
```

Example 5:

Searching by name on page 1 and choosing the return fields

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->readAll(['name' => "User test", 'page' => 1, 'fields' => [ 'user' => 'id,name' ] ]);

//Code equal to: (this option does not exist in the default laravel resource class)
$result = \App\Resources\UserResource::collection(\App\User::where('name',$params['name'])->paginate());
```

Disabling default methods:

Default methods: paginate and sort

```php
public function readAll(array $params = [], bool $collection = true)
{
return $this->mountRead(function() use ($params) {

//

}, $params, $collection ? UserResource::class : null, ['paginate','sort']);
}
```

Disabling Resource Class:

To disable the resource class simply enter the null value.

```php
public function readAll(array $params = [], bool $collection = true)
{
return $this->mountRead(function() use ($params) {

//

}, $params, null, []);
}
```

---

## Service Insert

Method to insertion data eloquent.

insert(array $values, array $exclude = [], array $include = [])

or

create(array $values, array $exclude = [], array $include = [])

Example 1:

Inserting and returning an array with the entered data

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->insert(['name' => "User test", "last_name" => 'other name']);

//Insert through the Create method (declare fields to insert into the model)
$result = $userService->create(['name' => "User test", "last_name" => 'other name']);

//Code equal to:
$model = new \App\User();
$model->name = 'User test';
$model->last_name = 'other name';
$model->save();
$result = $model->toArray();
```

Example 2:


Inserting and returning an array with the entered data.

Deleting or adding a field from the returned data.

```php
//Service - exclude return fields
$userService = \App\Services\UserService();
$result = $userService->insert(['name' => "User test", "last_name" => 'other name'], false, ['id','name']);

//Service - visible return fields
$userService = \App\Services\UserService();
$result = $userService->insert(['name' => "User test", "last_name" => 'other name'], false, [], ['id','name']);

//Code equal to:
$model = new \App\User();
$model->name = 'User test';
$model->last_name = 'other name';
$model->save();
$result = $model->makeHidden(['id','name'])->toArray(); //exclude return fields
$result = $model->makeVisible(['id','name'])->toArray(); //visible return fields
```

---

## Service Update

Method to update data eloquent.

updateById(int $id, array $values, array $exclude = [], array \$include = [])

Example:

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->updateById(1, ['name' => "New name"]);

//Code equal to:
$model = \App\User::where('id', 1);
$model->name = 'New name';
$model->save();
$result = $model->toArray();
```

---

## Service Delete

Method to delete data eloquent.

deleteById(int $id)

Example:

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->deleteById(1);

//Code equal to:
$result = \App\User::where('id', 1)->forceDelete();
```

---

## Service Soft Delete

Method to soft delete data eloquent.

softDeleteById(int $id)

Example:

```php
//Service
$userService = \App\Services\UserService();
$result = $userService->softDeleteById(1);

//Code equal to:
$result = \App\User::where('id', 1)->delete();
```

---

## Useful methods

File: \FzService\Service.php

```php
//Model
$this->modelClass;

//Create eloquent model instance
$this->newQuery();

//Set value field (array $params)
$this->setValuesModel(['name' => 'User test']);

//Save model (array $exclude = [], array $include = [], bool $returnArray = true)
$this->saveModel();

//Filter model (filtersModel(array $exclude = [], array $include = [], bool $returnArray = true))
$this->filtersModel();
```

---

## Contributing

Please see [CONTRIBUTING](https://github.com/FernandoZueet/service-laravel/graphs/contributors) for details.

## Security

If you discover security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Fernando Zueet](https://github.com/FernandoZueet)

## License

The package is licensed under the MIT license. See [License File](LICENSE.md) for more information.