|
Laravel 应工作需求: 1. 查询双数据库
2. 添加另一个库的默认返回字段 addtype ,并设置其默认值
这个就神奇了 参考网址:https://blog.csdn.net/qq_27295403/article/details/98872218
3. 修改另一个库的字段名称
使用
- select(
- "t1.id",
- "t1.tmNameText",
- "t1.tmIRNumberText as tmNumberText",
- "t1.tmClassText as ctype",
- "t1.dateApplication as registerDate",
- "t1.tmRightEndDate as specialEndDate",
- "t2.priceStatus"
- )
复制代码
4. 实现 双分页 [从第几条 取到 第几条 ,两个库返回值 搞一样 ,这样就实现了 双数据库 一个分页数据]
参考网址:https://blog.csdn.net/lxw1844912514/article/details/100029159
5. 拼凑数据 使用
array_merge()
来吧: battle
解决1 查询双数据库 ,个人建议 先添加配置 位置[config/database.php]
在'connections'中 添加 一条键值对
- // 权客云数据库地址与服务器 [该账户为只读账户]
- 'mysql_quancloud' => [
- 'driver' => 'mysql',
- 'url' => env('DATABASE_URL'),
- 'host' => '********',
- 'port' => '*******',
- 'database' => '******',
- 'username' => '********',
- 'password' => '*******',
- 'unix_socket' => env('DB_SOCKET', ''),
- 'charset' => 'utf8mb4',
- 'collation' => 'utf8mb4_unicode_ci',
- 'prefix' => '',
- 'prefix_indexes' => true,
- 'strict' => true,
- 'engine' => null,
- 'options' => extension_loaded('pdo_mysql') ? array_filter([
- PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
- ]) : [],
- ],
复制代码
好了 添加完了,然后在控制器中这么用:- DB::connection('mysql_quancloud')->table('trademark_detail ') ->where($bMap)->count();
复制代码
重要的就这么多了,然后开始上 控制器中全部代码:
- <?php
- namespace App\Http\Controllers\Out;
- use Illuminate\Routing\Controller as BaseController;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Input;
- use App\Model\CmsSellTrademark;
- use DB;
- // 对外暴露接口,查询可售商标
- class CanSellController extends BaseController
- {
- /**
- * 请求逻辑:
- * a : 本系统数据库 ;
- * b : 权客云数据库 ;
- *
- * 1. 带入请求条件,分别请求 a , b 内的符合条件的总数 aNum, bNum ;
- * 2. 页数,请求条数 为 必填值,如果不填 有默认值 page = 1 , limit= 20 ;
- * 3. page*limit = nowNum ; [为当前值]
- * 4. 判断 if( nowNum <= aNum){
- * ------如果请求数小于或等于a的总数----
- * [只取本地内的数据]
- * } else if(nowNum>aNum ){
- * ------如果请求数大于a的总数----
- *
- * if( nowNum < aNum+limit ){
- * -----且如果 处于ab之间,------
- * [则请求a 并且请求b,然后组装 ]
- * }else{
- * ----直接请求b------
- *
- * 从多少条起步,拿去limit的 长度 这样则不需要算页数;
- *
- *
- * }
- * };
- * 5. 返回值有limit, 有page 有 count , 有data [两者拼凑]
- *
- *
- *
- *
- *
- *
- */
- public function getsell(Request $request)
- {
- // 提取页码和条数 没有则使用默认值
- $page = (int) $request->input('page');
- if (empty($request->input('page'))) {
- $page = 1;
- }
- $limit = (int) $request->input('limit');
- if (empty($request->input('limit'))) {
- $limit = 20;
- }
- // 整合请求条件a
- $aMap = function ($query) use ($request) {
- $query->where('id', '>', 0);
- };
- // 整合请求条件b
- $bMap = function ($query) use ($request) {
-
- // $query->where('id', '>=', 50);
- $query->where('t2.priceStatus', '=', 2);
- };
- // 整合b的最终转换
- $bChangeArr = [
- DB::raw('"3" as addtype'),
- "t1.id",
- "t1.tmNameText",
- "t1.tmIRNumberText as tmNumberText",
- "t1.tmClassText as ctype",
- "t1.dateApplication as registerDate",
- "t1.tmRightEndDate as specialEndDate",
- "t2.priceStatus"
- ];
- // 获取 ab 的总数
- $aNum = CmsSellTrademark::where($aMap)->count();
- $bNum = DB::connection('mysql_quancloud')
- ->table('trademark_detail as t1')
- ->join('trademark_currency as t2', 't2.id', '=', 't1.businessId')
- ->where($bMap)
- ->count();
- // 判断当前总条数
- $nowNum = $page*$limit ;
- $resData = array();
-
- if ($nowNum <= $aNum) {
- // ------如果请求数小于或等于a的总数---- // return '只取本地内的数据';
- // 初始数据
- $surplus = $limit*($page-1);
- // 分页 会自动判断 参数中的page 值 这里只需要条数就可以
- $resData = CmsSellTrademark::where($aMap)->offset($surplus)->limit($limit)->get();
- } elseif ($nowNum>$aNum) {
- // ------如果请求数大于a的总数----
-
- if ($nowNum < $aNum +$limit) {
- // 初始数据
- $surplus = $limit*($page-1);
-
- // -----且如果 处于ab之间,------ // return '则请求a 并且请求b,然后组装';
- $aResData = CmsSellTrademark::where($aMap)->offset($surplus)->limit($limit)->get()->toArray();
- // 看一下剩余多少条
- $otherSurplus = $limit - count($aResData);
- $bResData = DB::connection('mysql_quancloud')
- ->table('trademark_detail as t1')
- ->join('trademark_currency as t2', 't2.id', '=', 't1.businessId')
- ->select($bChangeArr)
- ->where($bMap)
- ->offset(0)
- ->limit($otherSurplus)
- ->get()
- ->toArray();
- $resData = array_merge($aResData, $bResData);
- } else {
- // ----直接请求b------ // 从多少条起步,拿去limit的 长度 这样则不需要算页数 ,"t2.priceRange" priceRange为售价范围 ,priceStatus=2为售价开关 1未关 //return '直接请求b';
- $surplus = $limit*$page - $aNum;
- $resData = DB::connection('mysql_quancloud')
- ->table('trademark_detail as t1')
- ->join('trademark_currency as t2', 't2.id', '=', 't1.businessId')
- ->select($bChangeArr)
- ->where($bMap)
- ->offset($surplus)
- ->limit($limit)
- ->get();
- }
- };
- return [
- 'code' => 1,
- 'message' => '请求成功',
- 'count' => $aNum + $bNum,
- 'page' => $page ,
- 'limit' =>$limit,
- 'data' => $resData
- ];
- }
- /**
- * 设置可售的模式
- *
- */
- public function getAB()
- {
- }
- }
复制代码
|
|