通过优先级的单个laravel查询中的多选择查询

ktca8awb  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(347)

我想把这个查询语句转换成分页的laravel查询生成器

"SELECT images.*,categories.name,categories.slug   FROM ( SELECT 1 AS rnk, images.* FROM images WHERE title REGEXP '[[:<:]]{$q}[[:>:]]' 
                                UNION SELECT 2 AS rnk, images.* FROM images WHERE tags REGEXP '[[:<:]]{$q}[[:>:]]' ) images
                                inner join categories on categories.id = images.categories_id
                                where status = 'active' ORDER BY rnk"

我用的是拉威尔5.3

piztneat

piztneat1#

完成;)

$subquery  = "( SELECT 1 AS rnk, images.* FROM images WHERE title REGEXP '[[:<:]]{$q}[[:>:]]' 
                            UNION SELECT 2 AS rnk, images.* FROM images WHERE tags REGEXP '[[:<:]]{$q}[[:>:]]') query";
        $images    = Images::select(['categories.name', 'categories.slug', 'images.*'])
                        ->join('categories', 'images.categories_id', '=', 'categories.id')
                        ->join(\DB::raw($subquery), 'query.id', '=', 'images.id')
                        ->where('images.status', 'active' )
                        ->groupBy('images.id')
                        ->orderBy('query.rnk' )
                        ->paginate( $settings->result_request )
                        ->appends(request()->query());

相关问题