l5-repository基本使用

Posted zhengchuzhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了l5-repository基本使用相关的知识,希望对你有一定的参考价值。

一、安装

composer require prettus/l5-repository

技术分享图片

二、Model层:Warehouse.php

<?php

namespace AppModel;

use IlluminateDatabaseEloquentModel;

class Warehouse extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    protected $table = "supplier_warehouse";
    protected $primaryKey = g_w_id;
    public $timestamps = false;
}

三、仓库的两个文件:

3.1、文件及位置

技术分享图片

3.2、WarehouseRepository.php:

<?php

namespace AppRepositories;

use PrettusRepositoryContractsRepositoryInterface;

/**
 * Interface ShopRepository.
 *
 * @package namespace AppRepositories;
 */
interface WarehouseRepository extends RepositoryInterface
{
    public function getIdsByCmpId($cmpId, $sel);
    public function getNameById($g_w_id, $sel);
}

3.3、WarehouseRepositoryEloquent.php:

<?php

namespace AppRepositories;

use AppModelWarehouse;
use IlluminateSupportFacadesDB;
use PrettusRepositoryEloquentBaseRepository;

/**
 * Class ShopRepositoryEloquent.
 *
 * @package namespace AppRepositories;
 */
class WarehouseRepositoryEloquent extends BaseRepository implements WarehouseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Warehouse::class;
    }

    public function getIdsByCmpId($cmpId, $sel)
    {
        return $this->model
            ->select(DB::raw(implode(,,$sel)))
            ->where(company_id,$cmpId)
            ->get();
    }

    public function getNameById($g_w_id, $sel)
    {
        return $this->model
            ->select(DB::raw(implode(,,$sel)))
            ->where(g_w_id,$g_w_id)
            ->get();
    }
}

四、绑定:

4.1、文件及位置:

技术分享图片

4.2、RepositoryServiceProvider.php:

$this->app->bind(AppRepositoriesWarehouseRepository::class, AppRepositoriesWarehouseRepositoryEloquent::class);

五、services

5.1、文件及位置:

技术分享图片

5.2、WarehouseService.php:

<?php

namespace AppServices;

class WarehouseService
{
    private $warehouseRepository;

    public function __construct($warehouseRepository)
    {
        $this->warehouseRepository = $warehouseRepository;
    }

    public function getIdsByCmpId($cmpId)
    {
        return $this->warehouseRepository->getIdsByCmpId($cmpId, [g_w_id]);
    }

    public function getNameById($g_w_id)
    {
        return $this->warehouseRepository->getNameById($g_w_id, [warehouse_name]);
    }
}

六、控制器中调用:

6.1、文件及位置

技术分享图片

6.2、WarehouseController.php

<?php

namespace AppHttpControllersWarehouse;

use AppRepositoriesWarehouseRepository;
use AppServicesWarehouseService;
use IlluminateHttpRequest;
use AppHttpControllersController;

class WarehouseController extends Controller
{
    private $warehouseService;

    public function __construct(WarehouseRepository $warehouseRepository)
    {
        $this->warehouseService = new WarehouseService($warehouseRepository);
    }

    public function test()
    {
        $cmpId = 1016;
        $listWarehouse = $this->warehouseService->getIdsByCmpId($cmpId);
    }
}

 七、在config/app.php上加载组件:

技术分享图片

代码:

AppProvidersRepositoryServiceProvider::class,

 

以上是关于l5-repository基本使用的主要内容,如果未能解决你的问题,请参考以下文章

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

c_cpp Robolution基本代码片段

如何从片段外部清除/重置地图?

JavaScript 代码片段

Android 使用两个不同的代码片段获取当前位置 NULL

(转) Java中的负数及基本类型的转型详解