dongrao1862 2019-07-06 18:34
浏览 116

如何在Laravel中使用DB :: select union查询生成分页

In my project I have been used DB:select with UNION six table and ORDER BY . How can I add pagination to this query in the Laravel framework?

I already try with Laravel documentation pagination, but it cannot get pagination with six table UNION query.

public function publishAds()
{
    $id = Auth::user()->id;
    $data = City::all();
    $ads = DB::select("(SELECT id,title,updated_at,'Commercial Properties' as 
        'type',town,city,rent_per_month,image1,add_status,'commercial_properties' as 
        tableName,type as adType  from commercial_properties WHERE add_status='published' 
        AND user_id=$id) union (select id,title,updated_at,'House',town,city,rent_per_month,
        image1,add_status,'houses',type from houses WHERE add_status='published' 
        AND user_id=$id ) union (select id,title,updated_at,'Land',town,city,rent_per_month,
        image1,add_status,'lands',type from lands WHERE add_status='published' 
        AND user_id=$id ) union (select id,title,updated_at,'Apartment',town,city,
        rent_per_month,image1,add_status,'apartments',type from apartments 
        WHERE add_status='published' AND user_id=$id ) union (select id,title,updated_at,
        'Holyday Rental',town,city,rent_per_month,image1,add_status,'holyday_rentals',
        type from holyday_rentals WHERE add_status='published' AND user_id=$id ) union 
        (select id,title,updated_at,'Room',town,city,rent_per_month,image1,add_status,
        'rooms',type from rooms WHERE add_status='published' AND user_id=$id ) order 
        by updated_at DESC,rand()");

    return view('admin/my-add', ['ads' => $ads, 'cities' => $data, 'status' => 'Publish Ads']);
}

I want to get pagination for this query which includes six UNION.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 yolov8边框坐标
    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真