Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vinay22's avatar

How to give priority to one query over another in multiple union in Laravel 5

I am writing query that return search results from multiple union statements. Query is running fine and results are showing but results are randomly showing. I want them as per some priority. I know how to do it in core php. how can i do this in laravel 5.

SearchController.php :

     $first = DB::table('info')
    ->select('id','name')
    ->where('name', 'LIKE', $searchstring)
    ->union($second)
    ->union($third)
    ->union($fourth)
    ->union($fifth)
   // ->orderBy('sb')
    ->get(); 

in core php i did this

 (SELECT '1' as sb, id where name LIKE ? )
            union
 (SELECT '2' as sb, id, ...) 
          union
 (SELECT '3' as sb, id, ...) 
            union
 (SELECT '4' as sb, id, ...)
            union
 (SELECT '5' as sb, id, ...) order by sb ";

sb is not any column in my table. how can I write this in laravel?

0 likes
0 replies

Please or to participate in this conversation.