简体   繁体   中英

multiple select query in single laravel query via Priority

i want convert this query statement to laravel query builder with pagination

"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"

i use laravel 5.3

Done ;)

            $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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM