1.关联查询
模型中写好对应关系,如下:
public function partners()
{
return $this->hasOne('App\PartnersDesc', 'partner_id', 'partner_id');
}
控制器中写如下:
$where['p_status'] = $prostatus;
$product = Product::with(['partners' => function($query) {
$query->select('partner_name', 'partner_short_name', 'partner_id');
}])
->whereHas('partners', function ($query1) use ($partner_name)
{
$query1->where('partner_name','like', '%'.$partner_name.'%');
})
->where($where)
->orderBy('p_id','desc')
->skip(($currentCount)*$pageSize)
->take($pageSize)
->get()
->toArray();
dd($product);die;
2.laravel中的where in查询
$return = MyReturn::with(['OrderBorrowApply'=>function($query){
$query->select('b_id','p_id');
}])->whereIn('r_id',$rids)
->first(['r_id','b_id','r_sum']);
dd($return);
3.聚合函数查询
$count = MyReturn::where("b_id",$return['b_id'])->whereIn('r_status' ,[1,3])->count("r_id");
$sum = MyReturn::whereIn('r_id',$rids)->sum('r_sum');
4.获取正在执行的SQL语句
DB::enableQueryLog();
$updateBorrowData["b_status"] = 5000;
$borrowres = OrderBorrowApply::where("b_id",$return["b_id"])->update($updateBorrowData);
dd(DB::getQueryLog());
5.获取以某一个为字段为健,一个为值的语句
$ids = PartnersDesc::where('partner_name','like','%'.$partnername.'%')->pluck('partner_id','id')->toArray();
完整的列表方法
public function index(Request $request) { $paramInput = $request->all(); //获取模型 $Product = Product::query(); //根据机构名称获取机构ID $partner_name = $paramInput['pname']; if(!empty($partner_name)){ $ids = PartnersDesc::where('pname','like','%'.$partner_name.'%')->pluck('id')->toArray(); //机构名称条件查询 $Product ->whereIn('pid',$ids); } //课程名称获取数据 $pproductname = $paramInput['p_name']; if(!empty($pproductname)){ $Product ->where('p_name','like','%'.$pproductname.'%'); } //课程状态获取数据 $prostatus = $paramInput['p_status']; if($prostatus != null){ $Product ->where('p_status',$prostatus); } $currentCount = $request->input('current_count') ?: 1; $pageSize = $request->input('count') ?: 15; //获取总数 $total = $Product->count(); $product = $Product ->with(['partners' => function($query) { $query->select('partner_name', 'partner_short_name', 'partner_id','id'); }]) ->orderBy('p_id','desc') ->skip(($currentCount-1)*$pageSize) ->take($pageSize) ->get() ->toArray(); // DB::enableQueryLog(); // return DB::getQueryLog(); $data = [ 'page_data' => $product, 'total' => $total, ]; return MaYiResponse::response($data); }
生成不重复的机构编号
//机构编号生成规则 100000 + 随机六位数
$partner_no = DB::select('select group_concat(partner_no) as partner_no from my_partners');
$str = array_diff(range(100000, 999999), explode(',', $partner_no['partner_no']));
shuffle($str);
laravel查看框架版本:php artisan --version