鍍金池/ 問(wèn)答/PHP/ laravel下載數(shù)據(jù)關(guān)聯(lián)模型數(shù)據(jù)多超出mysql占位數(shù)?

laravel下載數(shù)據(jù)關(guān)聯(lián)模型數(shù)據(jù)多超出mysql占位數(shù)?

1、查詢用戶列表,初始換關(guān)聯(lián)訂單表,但是用戶太多,導(dǎo)致mysql占位符過(guò)了,報(bào)mysql錯(cuò)誤。

2、User::with('order')->get()

報(bào)錯(cuò):General error: 1390 Prepared statement contains too many placeholders

select * from order where user_id in (xxx,xxx , ......)
因?yàn)楹罄m(xù)需要循環(huán)處理訂單相關(guān)聯(lián)的數(shù)據(jù)。
foreach ($user as $v) {

$order_list = $v->order;
$temp_test = [];
if (count($order_list) > 0) {
    foreach ($order_list as $ov) {
        $temp_test[] = $ov->order_id;
    }
}
$data = [
    'user_id' => $v->user_id,
    'order_id' => implod(',', $temp_test)
];
echo $this->downExcel($data);

}

回答
編輯回答
壞脾滊

使用chunk即可。

2018年6月21日 05:32