将原始的mySQL语句转换为具有分页功能的laravel 4

时间:2016-05-25 10:17:46

标签: laravel pagination eloquent

我试图用分页法在Eloquent中表达MySQL语句

代码:

$query = "(SELECT city_name FROM Cities WHERE city_name LIKE '%" . 
   $keyword . "%') 
   UNION
   (SELECT area_name FROM Areas WHERE area_name LIKE '%" . 
   $keyword . "%') 
   UNION
   (SELECT sub_location_name FROM Sub_locations WHERE sub_location_name LIKE '%" . 
   $keyword . "%' )";

1 个答案:

答案 0 :(得分:0)

试试这个:

$cities = DB::table('Cities')->select('city_name')->where('city_name', $keyword);

$area=DB::table('Areas')->select('area_name')->where('area_name', $keyword);

$result=DB::table('Sub_locations')
        ->select('sub_location_name')
        ->where('sub_location_name', $keyword)
        ->union($cities)
        ->union($area)
        ->paginate(15);