laravel 三表统计的终极版

来源:互联网 发布:阿里云百倍故障赔偿 编辑:程序博客网 时间:2024/05/22 05:18
class IndexController extends Controller
{
//\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php
//    public function __construct ($items,$total,$perPage,$currentPage=null,array $options=[]){
//        foreach ($options as $key => $value) {
//            $this->{$key}=$value;
//        }
//        $this->total=$total;
//        $this->perPage=$perPage;
//        $this->lastPage=(int) ceil($total / $perPage);
////        $this->path=$this->path!= '/' ? rtrim($this->path,'/');
//        $this->currentPage=$this->setCurrentPage($currentPage,$this->pageName);
//        $this->items=$items instanceof Collection ? $items : Collection::make($items);
//
//    }


    public function index(Request $request)
    {


            $name = $request->input('name');


//            $where = ' WHERE 1';
//            if ($name) {
//                $where = ' WHERE name like ' . "'%$name%'";
//            }
           $paginate = 15;
            $goods=Goods::select(DB::Raw('goods.*,COUNT(goods_nexus.rfid) as nums,goods_class.class_name'))
            ->leftjoin('goods_nexus','goods_nexus.goods_id','=','goods.id')
            ->leftjoin('goods_class','goods_class.id','=','goods.class_id')
            ->groupBy('goods.id')->where('name', 'like', '%' . $name . '%')->paginate($paginate);


//            dd($goods);
        $count =Goods::where('name', 'like', '%' . $name . '%')->count();


          $total_page = ceil($count / $paginate); //共多少页
//            $goods = DB::select('SELECT COUNT(gn.rfid) as nums,pro.* FROM goods_nexus
//              as gn RIGHT JOIN (SELECT g.* ,gc.class_name FROM goods as
//              g LEFT JOIN goods_class as gc  ON g.class_id = gc.id)as pro ON
//               pro.id = gn.goods_id ' . $where . ' GROUP BY pro.id ASC ');


//            $total = count($goods);
//            $goodslist = new \Illuminate\Pagination\LengthAwarePaginator($goods, $total, 10);
//            $total_page = ceil($total / 10);


//    dd($goods);
            return view('index',[
                'name' => $name,
                'total_page'=>$total_page,
            ])
                ->with('goods', $goods);
//                ->with('goods', $goodslist)->with('total_page', $total_page);


    }


}