• 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基本使用--结合使用artisan

  • 相关阅读:
    python笔记第十一天 模块补充
    python笔记第十天 模块
    python笔记第九天 装饰器
    python笔记第八天 迭代器与生成器
    python笔记第七天 文件操作
    python笔记第六天 函数和函数的内置方法
    C语言----指针形参(指向指针的指针形参)
    NEON使用方法
    ARM NEON指令集总结
    三维变换矩阵左乘和右乘分析
  • 原文地址:https://www.cnblogs.com/zhengchuzhou/p/9924607.html
Copyright © 2020-2023  润新知