分页+排序
//forPage('当前页码','每页多少条数据') latest()传入唯一参数进行desc排序$query->forPage($page, $pagesize)->latest('created_time');
指定顺序排序
//文章表中->分类字段: group_ids示例值 ,1,2,3,5,
$search_gids =1;//要查询的分组id
$res = Article::whereRaw(" FIND_IN_SET(id,'3,2,5,4,7,8,1,12')") // 等价 whereIn('id',[3,2,5,4,7,8])
->whereRaw(" FIND_IN_SET(".$search_gids.",group_ids)") //等价 where('group_ids','like',"%,{$search_gids},%")
->orderBy(DB::raw(" field(id,3,2,5,4,7,1,12)"))//指定排序规则 (未指定排序的记录会在队首)
->get(['group_ids'])->toArray();
dd($res);
//注意:参数格式 find_in_set(column,'str_list'),field(column,val1,val2,val3);
like是广泛的模糊匹配,字符串中没有分隔符,find_in_set是精确匹配,字段值以英文”,”分隔,且find_in_set 是全表扫描的
表子查询+行子查询
$res = DB::table(DB::raw("(select * from merchant where id<10) as t"))->whereIn('id',function($query)use($id){
$query->from('merchant')->select('id')->where('id','=',$id);//行子查询
})->get();
//注意: 行子查询,传参需要用闭包传参的方式,子查询选择表时用from('表名')。
其它://一. 变量不为空时,定义
!defined('ORDER_SALEPAY') && define('ORDER_SALEPAY',5);
这篇博客探讨了在Laravel ORM中如何进行高级操作,包括分页、指定顺序排序以及使用表子查询和行子查询。通过这些技巧,开发者可以更高效地处理和检索数据。
1984

被折叠的 条评论
为什么被折叠?



