prophecy/servicepacker

dev-master 2019-09-28 04:47 UTC

This package is auto-updated.

Last update: 2020-05-28 06:09:01 UTC


README

Build Status

Service Packer is a package for Service & Repository Pattern. Rather than to rewrite or copy files from the old project,its better to make a simple package and upgrade it overtime from the experience I gather.

Installation

composer require prophecy/servicepacker

Registering Artisan Commadns

Add the following line in your app/Console/Kernel.php

use Prophecy\ServicePacker\Console\{GenerateService,GenerateRepository};

and in the $commands Array Add

    GenerateService::class,
    GenerateRepository::class,

The end product should look something like this

protected $commands = [
    GenerateService::class,
    GenerateRepository::class,
];

Using the artisan cli to craft a repository

php artisan make:repo User

it will create a UserRepository In your "app/Repositories" directory By default it will extend from the base repository of the package

Using the artisan cli to craft a service

php artisan make:service User

it will create a UserService In your "app/Services" directory By default it will extend from the base service of the package

If you want to use the default CRUD operations in the service, you can use the crudable flag

php artisan make:service User crudable

It will use a trait called crudable that will give you the basics of CRUD functions

he create,read,edit,update,delete etc should be assigned with one responsibility.If you have Users & Posts and their create data should be different along with its validation. Thus the validity should be done in your end and should be handled by other functions. Thus these common functions were separated.

API

Repository

  • instance() returns the model instance ($this->model)

  • create($array) method takes an array of data and uses eloquent's create. Equivalent to Model::create()

  • CreateMultiple($arrayOfArray) methods takes an array of array,If you want to store multiple records at one.

  • find($id) takes the primaryKey from the Model and finds by that primary key.

  • __ Note : If you have a different primary key column,for example user_id instead of id, you can set the primaryKey property in the model,that way you won't have any error.

  • findFirst(array $conditions,$select) Takes a array or array of array of conditions and a $select.$select can be a string or array.it searches based on those conditions,if found,selects the first record and selects the specific columns. $select is by default programmed to select all columns.

  • getWhere($conditions,$select,$paginate) takes an array or array of array of condtions,$select and a paginate variable,paginate is set to zero by default,so it will return all records.

  • getWhereCount($condtions) takes an array or array of array and counts the number of rows based on that search

  • first() returns the first row of the tabel

  • latest() returns all record and orders them in a descending manner based on the created_at column

  • last() returns the last row of the column

  • all() returns all records of the table

  • update(array $attributes,int $primaryKey) takes a array of attributes and a primarykey,searches the database using the find($id) and updates the columns if found.

  • delete($primaryKey) deletes based on the search result using the primary key.

  • whereHas(array $conds,$list,$select) takes the $conditions array or array of array and the $select like before,but as a second parameter it also takes a $list.At first it will search for all the records based on the conditions,select the based on the $select value and run a $collection->has($list) . Returns Collection

  • getFiltered(array $conds,$callback,$select,$paginate) takes array or array of array of conditions,$select as before,then also a $callback,and filters them based on the callback.Then paginate or returns all of them.