Laravel Oh Generators

Build Status Total Downloads Latest Stable Version License License
This package extends the core file generators that are included with Laravel 5.

Installation

via composer

composer require yish/generators
  • 5.4 before using branch: 1.1.x
  • 5.5 before using branch: 2.0.x

Registering Service Provider

If you are using laravel 5.5 or later, you can use auto discovery, also, you don't need put in service provider to app.php.

'providers' => [
    \Yish\Generators\GeneratorsServiceProvider::class,
],

Generating Service

It can be generating class service.

php artisan make:service UserService

Supported abstract Service class, and provide following:

  • all()
  • create($attributes)
  • first()
  • firstBy($column, $value)
  • find($id)
  • findBy($column, $value)
  • get()
  • getBy($column, $value)
  • update($id, $attributes)
  • updateBy($column, $value, $attributes)
  • destroy($id)
  • destroyBy($column, $value)
  • paginate($page = 12)
  • paginateBy($column, $value, $page = 12)

Generating Repository

It can be generating class repository.

php artisan make:repository UserRepository

Supported abstract Repository class, and provide following:

  • all($columns = ['*'])
  • create($attributes)
  • update($id, array $attributes, array $options = [])
  • updateBy($column, $value, array $attributes = [], array $options = [])
  • first($columns = ['*'])
  • firstBy($column, $value, $columns = ['*'])
  • find($id, $columns = ['*'])
  • findBy($column, $value, $columns = ['*'])
  • get($columns = ['*'])
  • getBy($column, $value, $columns = ['*'])
  • destroy($ids)
  • destroyBy($column, $value)
  • paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  • paginateBy($column, $value, $perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

Generating Transformer

It can be generating class transformer.

php artisan make:transformer UserTransformer

Support helper and contract

// $instance => Transformer class.
// $attributes => Need transform data, maybe array or collection etc.
transform(UserTransformer::class, $data);
namespace App\Transformers;

use Yish\Generators\Foundation\Transform\TransformContract;

class UserTransformer implements TransformContract
{
    public function transform($attributes)
    {
        //
    }
}

Generating Formatter

It can be generating class formatter.

php artisan make:formatter UserFormatter

support helperStatusable and FormatContract

// $request => Must instance of Illuminate\Http\Request.
// $instance => Formatter class.
// $items => data.
format(request(), UserFormatter::class, $data);
namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    public function format(Request $request, $items = [], $message = '', $status = 200)
    {
        //
    }
}

You can use Statusable trait to help you rapid create formatted. set property $status = true and then get success format, $status should be type boolean, otherwise you will get exception.

namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    use Statusable;
    protected $status = true;
}

If set $status = false, return fail format.

class PostFormatter implements FormatContract
{
    use Statusable;
    protected $status = false;
}

If you need to customize return message, you can do overwrite message method.

class PostFormatter implements FormatContract
{
    use Statusable;
    protected $status = false;
    
    public function message()
    {
        return 'hello world'.
    }
}

Also you can customize return code via overwrite code method.

class Success extends Formatter implements FormatContract
{
    use Statusable;
    protected $status = false;

    public function code()
    {
        return Response::HTTP_ACCEPTED;
    }
}

More detail -> Yish\Generators\Foundation\Format\Statusable

Generating Presenter

It can be generating class presenter.

php artisan make:presenter UserPresenter

Generating Foundation

It can be generating class foundation.

php artisan make:foundation Taggable
Last Updated: 1/27/2019, 1:25:04 PM