首页 > 其他 > 详细

l5-repository基本使用

时间:2018-11-07 19:23:15      阅读:451      评论:0      收藏:0      [点我收藏+]

一、安装

composer require prettus/l5-repository

技术分享图片

二、Model层:Warehouse.php

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

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 App\Repositories;

use Prettus\Repository\Contracts\RepositoryInterface;

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

3.3、WarehouseRepositoryEloquent.php:

<?php

namespace App\Repositories;

use App\Model\Warehouse;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;

/**
 * Class ShopRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
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(\App\Repositories\WarehouseRepository::class, \App\Repositories\WarehouseRepositoryEloquent::class);

五、services

5.1、文件及位置:

技术分享图片

5.2、WarehouseService.php:

<?php

namespace App\Services;

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 App\Http\Controllers\Warehouse;

use App\Repositories\WarehouseRepository;
use App\Services\WarehouseService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

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上加载组件:

技术分享图片

代码:

App\Providers\RepositoryServiceProvider::class,

 

l5-repository基本使用

原文:https://www.cnblogs.com/zhengchuzhou/p/9924607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!