美文网首页
laravel -- Eloquent ORM

laravel -- Eloquent ORM

作者: Sharise_Mo佩珊 | 来源:发表于2018-10-11 18:41 被阅读0次

    查询

     if ($request->filled('keyword')) {
                $store_ids = Store::where([
                    'business_no' => $this->business_no,
                    ['store_name', 'LIKE', '%' . $request->keyword . '%'],
                ])->pluck('id')->toArray();
    
                $guide_ids = StoreGuide::where([
                    'business_no' => $this->business_no,
                    ['guide_name', 'LIKE', '%' . $request->keyword . '%'],
                ])->pluck('id')->toArray();
    
                $customer_ids = Customer::where([
                    'business_no' => $this->business_no,
                ])->where(function ($query) use ($request) {
                    $query->orWhere('customer_name', 'LIKE', '%' . $request->keyword . '%');
                    $query->orWhere('phone', 'LIKE', '%' . $request->keyword . '%');
                })->pluck('id')->toArray();
    
                $query->where(function ($query) use ($request, $store_ids, $guide_ids, $customer_ids) {
                    $query->orWhere('order_no', 'LIKE', '%' . $request->keyword . '%');
                    if (!empty($store_ids)) {
                        $query->orWhereIn('store_id', $store_ids);
                    }
                    if (!empty($guide_ids)) {
                        $query->orWhereIn('guide_id', $guide_ids);
                    }
                    if (!empty($customer_ids)) {
                        $query->orWhereIn('customer_id', $customer_ids);
                    }
                });
            }
            $query->with([
                'customer' => function ($query) {
                    $query->select('id', 'customer_name', 'nickname', 'phone', 'head_image');
                },
                'guide'    => function ($query) {
                    $query->select('id', 'guide_name', 'guide_nickname', 'phone');
                },
                'goods'    => function ($query) {
                    $query->select('order_no', \DB::raw('Sum(number) as number'))->groupBy('order_no');
                },
                'store'    => function ($query) {
                    $query->select('id', 'store_name', 'store_no');
                },
                'activity'    => function ($query) {
                    $query->select('id', 'activity_no');
                },
            ]);
            $query->select($fields)->orderBy('order_time','desc');
            $orders = $query->paginate($page_size)->toArray();
    
    $query = Customers::query()->where([
                'business_no' => $this->business_no,
                'is_delete'   => 0,
            ]);
            $query->whereHas('guide', function ($query) use ($request) {
                $query->where('guide_id', $request->guide_id);
            });
    

    查询某个月份生日的会员:sql语句

    // 查询某个月份生日的会员:sql语句
    SELECT * FROM `user_detail` where FROM_UNIXTIME(UNIX_TIMESTAMP(birthday), "%m")=10 ORDER BY ctime DESC
    
    

    laravel 数据 导出 到excel

    • 注意:3.1版本的excel导出没有create方法了,需要用旧版本2.1或以下 $ composer require maatwebsite/excel ~2.1
    先安装完成excel扩展,然后在config里面添加两条excel的配置和别名
    /**
         * 测试导出到excel功能
         */
        public function exportToExcel(){
            $cellData = [
                ['学号','姓名','成绩'],
                ['10001','AAAAA','99'],
                ['10002','BBBBB','92'],
                ['10003','CCCCC','95'],
                ['10004','DDDDD','89'],
                ['10005','EEEEE','96'],
            ];
    //        Excel::create('users',function($excel) use ($cellData){
    //            $excel->sheet('score', function($sheet) use ($cellData){
    //                $sheet->rows($cellData);
    //            });
    //        })->export('csv');
    
            // 导出 Excel 并能直接在浏览器下载
            $export_file_name = 'student';
    //        Excel::create($export_file_name, function ($excel) {
    //            $excel->sheet('score', function ($sheet) {
    //                $sheet->appendRow(['data 1', 'data 2']);
    //                $sheet->appendRow(['data 3', 'data 4']);
    //                $sheet->appendRow(['data 5', 'data 6']);
    //            });
    //        })->download('csv');
    
            // 导出 Excel 并存储到指定目录
            Excel::create($export_file_name, function ($excel) use ($cellData) {
                $excel->sheet('score', function ($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            })->store('csv', 'D:/');
        }
    

    laravel 合成图片

    /**
         * 合成图片
         * 将导购头像和二维码,以及背景图合成成一个海报图
         */
        public function composePoster(Request $request){
            $guide = $this->guide;
            $store_name = $guide['store_name'];
            $business_no = $this->business_no;
    
            // 圆形头像
            $client = new \GuzzleHttp\Client();
            $avatarResponse = $client->get($guide['head_image']);
            $avatar = \Image::make($avatarResponse->getBody()->getContents());
    
            $img =Image::make($avatar)->resize(148,148);
            $new = Image::canvas(148,148,'#fff');
            $r=$img->width() /2;
    
            for($x=0;$x<$img->width();$x++) {
                for($y=0;$y<$img->height();$y++) {
                    $c=$img->pickColor($x,$y,'array');  // 取色
                    if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
                        $new->pixel($c,$x,$y);   // 根据每个像素点的颜色画图
                    }
                }
            }
            $avatar = $new ;
    
            $file_path = 'https://crm.pinkr.com/'.urlencode($guide['qrcode']);  // 二维码
    
            $qrcode = Image::make($file_path)->fit(450,450);
            $img_bg = Image::make(public_path('poster-bg.jpg'));
    
            $img_bg->insert($avatar,'top-center',0,160);
            $img_bg->insert($qrcode,'bottom-center',0,400);
    
            $img_bg->text($guide['guide_name'], 380, 350, function($font) {
                $font->file(public_path('SimHei.ttf'));
                $font->size(25);
                $font->color('#000');
                $font->align('center');
                $font->valign('top');
            });
    
            $img_bg->text('-  '.$store_name.'  -', 380, 425, function($font) {
                $font->file(public_path('SimHei.ttf'));
                $font->size(20);
                $font->color('#fff');
                $font->align('center');
                $font->valign('top');
            });
    
            // 线上
            $img_bg->save(public_path('/poster/poster_'.$guide['guide_name'].'.jpg'));
    
            $res = 'https://crm.xxx.com/poster/poster_'.$guide['guide_name'].'.jpg';
    
            return $this->returnJsonMsg(200,$res);
    
    //        本地
    //        $img_bg->save(public_path('poster_'.$guide['guide_name'].'.jpg'));
    //        return $img_bg->response('jpg');
        }
    

    相关文章

      网友评论

          本文标题:laravel -- Eloquent ORM

          本文链接:https://www.haomeiwen.com/subject/occiaftx.html