Laravel - Union + Paginate at the same time?

后端 未结 12 593
有刺的猬
有刺的猬 2020-12-09 04:20

Brief:

I am trying to union 2 tables recipes and posts then add ->paginate(5) to the queries.

But

12条回答
  •  醉话见心
    2020-12-09 04:45

    For those who may still look for the answer, I have tried union and paginate together and got right result under laravel 5.7.20. This will be better than merging collections then paginate which will not work on big amount of data.

    Some demo code (in my case, I will deal with multiple databases with same table name):

    $dbs=["db_name1","db_name2"]; 
    $query=DB::table("$dbs[0].table_name");
    for($i=1;$iunion($query);
    }
    $query=$query->orderBy('id','desc')->paginate(50);
    

    I haven't tried on other higher version of laravel. But at least it could work now!

    More information

    My previous version of laravel is 5.7.9 which will report the Cardinality violation error. So the laravel team solved this issue in some version of 5.7.x.

提交回复
热议问题