omnicode/lara-service

4.0.1 2018-10-14 17:04 UTC

This package is auto-updated.

Last update: 2024-09-29 04:49:37 UTC


README

Build Status Total Downloads Latest Stable Version License

Lara-Service

通用服务层

安装

从您的终端运行以下命令

composer require "omnicode/lara-service: 2.0.*"

或者将此添加到您的 composer.json 文件的 require 部分

"omnicode/lara-service": "2.0.*"

然后运行 composer update

使用方法

首先,创建您的 Service 类,如下所示的示例 AcccountService

<?php

namespace App\Services;

use App\Repositories\Contracts\AccountRepositoryInterface as AccountRepository;
use App\Validators\AccountValidator;
use LaraService\Services\LaraService;

class AccountService extends LaraService
{
    public function __construct(AccountRepository $accountRepository, AccountValidator $accountValidator)
    {
        $this->baseRepository = $accountRepository;
        $this->baseValidator = $accountValidator;
    }
}

Repository 和 Validator 类应该具有以下方法 pushCriteria, saveAssociated, findForShow, findFillable, getKeyName, destroy, getIndexableColumns, paginate, setSortingOptionsisValid, getErrors 分别。这些已在 Lara ValidationLara Repository 包中实现。

最后,在控制器中使用服务

<?php

namespace App\Http\Controllers;

use App\Services\AccountService;

class AccountsController extends Controller
{   
    public function __construct(AccountService $accountService)
    {
        parent::__construct();
        $this->baseService = $accountService;
    }
}

可用方法

以下方法在 LaraService 中可用

LaraRepo\Contracts\RepositoryInterface
    public function setBaseRepository(RepositoryInterface $repository)
    public function getBaseRepository()
    public function setBaseValidator($validator)
    public function getBaseValidator ()
    public function setValidationErrors($errors)
    public function getValidationErrors()
    public function paginate($sort = [], $group = self::GROUP)
    public function create($data)
    public function createWithRelations($data, $relations = null)
    public function findForShow($id, $columns = null)
    public function find($id)
    public function update($id, $data)
    public function updateWithRelations($id, $data, $relations = null)
    public function destroy($id)
    public function validate($validator, $data, $options = [])
    public function paginateRepositoryWhere($repository, $group = self::GROUP, $column = null, $val = null)
    public function paginateRepository($repository, $group = self::GROUP)
    public function setSortingOptions($repository, $options = [], $group = self::GROUP)
}

示例 - 创建一个新的账户仓库

    
    // for index page use
    // returns item list and columns with their columns information 
    $this->accountService->paginate()
      
    // if you want to sort your index page use
    $this->accountService->setSortingOptions($repository)
    
    // to create new record
    $this->accountService->create($request->all());
    
    // returns validation errors of last operation
    $this->accountService->getValidationErrors();
    
    // to save items with relations
    $this->accountService->createWithRelations($data, $relations)
    
    // to find based on $showable attributes
    $this->accountService->findForShow($id)
    
    // usual find with fillable columns
    $this->accountService->find($id)
    
    // to update based on primary key
    $this->accountService->update($id, $data)

    // to update and item with relations
    $this->accountService->updateWithRelations($id, $data, $relations)
    
    // to delelte
    $this->accountService->destroy($id)
    
    // to validate the data (internally uses the injected Validator)
    $this->accountService->validate($validator, $data, $options = [])