Skip to content
关注公众号,获取新课通知

商品列表


控制器 app/controller/admin/Goods.php

php
// 后台商品列表
   public function index()
   {
   	
   	$param = request()->param();
       $limit = intval(getValByKey('limit',$param,10));
       $tab = getValByKey('tab',$param,'all');
       $model = $this->M;
       // 订单类型
       switch ($tab) {
       	case 'checking': // 审核中
       		$model = $this->M->where('ischeck',0)
       						 ->whereNull('delete_time');
       		break;
       	case 'saling': // 销售中
       		$model = $this->M->where('ischeck',1)
       						 ->where('status',1);
       		break;
       	case 'off': // 已下架
       		$model = $this->M->where('status',0);
       		break;
       	case 'min_stock': // 库存预警
       		$model = $this->M->where('status',0)
       						 ->whereColumn('stock','<=','min_stock');
       		break;
       	case 'delete': // 回收站
       		$model = $this->M->onlyTrashed();
       		break;	
       }
       // 搜索条件
       if (array_key_exists('category_id',$param)) {
       	$model = $model->where('category_id',$param['category_id']);
       }
       if (array_key_exists('title',$param)) {
       	$model = $model->where('title','like','%'.$param['title'].'%');
       }
       
       $totalCount = $model->count();
       $list = $model->page($param['page'],$limit)
	        ->with(['category','goodsBanner','goodsAttrs','goodsSkus','goodsSkusCard.goodsSkusCardValue'])
	        ->order([ 'id'=>'desc' ])
			->select();
	// 分类
       $cates = (new \app\model\Category())->select();
       $cates = list_to_tree($cates->toArray(),'category_id');
       return showSuccess([
       	'list'=>$list,
       	'totalCount'=>$totalCount,
       	'cates'=>$cates
       ]);
   }

模型 app/model/Goods.php

php
	// 设置json类型字段
    protected $json = ['sku_value'];
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    
    // 转化json字段
    public function setSkuValueAttr($value,$data){
        if (!empty($value)) {
            return [
                "oprice" => intval($value['oprice']),
                "pprice" => intval($value['pprice']),
                "cprice" => intval($value['cprice']),
                "weight" => intval($value['weight']),
                "volume" => intval($value['volume']),
            ];
        }
    }

    
    // 关联分类
    public function category(){
    	return $this->belongsTo('Category');
    }
    
    // 关联评论
    public function comments(){
        return $this->hasMany('OrderItem')->whereNotNull('rating');
    }
    
    // 关联规格信息
    public function goodsSkus(){
        return $this->hasMany('GoodsSkus'); 
    }

    // 商品规格卡片
    public function goodsSkusCard(){
        return $this->hasMany('GoodsSkusCard')->order([
        	'order'=>'ASC'
        ]);
    }

    // 商品属性
    public function goodsAttrs(){
        return $this->hasMany('GoodsAttrs');
    }

    // 关联商品轮播图
    public function goodsBanner(){
        return $this->hasMany('goodsBanner');
    }

验证器 app/validate/admin/Goods.php

php
protected $rule = [
    'page' => 'require|integer|>:0',
];
protected $scene = [
    ...
    'index'=>['page'],
    ...
];

路由 router/admin.php

php
Route::get('goods/:page','admin.goods/index');