2 回答

TA貢獻1886條經驗 獲得超2個贊
這是由于附加查詢邏輯的工作方式所致。當您將更改附加到查詢的一個版本時,它會修改原始版本,因此來自基礎的任何后續查詢也會受到影響。您應該能夠使它與clonePHP 中的關鍵字一起使用:
$baseQuery = \DB::table('device_inventories')->where([
'device_companies.company_id' => session()->get('COMPANY_ID'),
'device_inventories.device_id' => $Self->id,
])->select([
...
])
->join('devices', 'devices.id', '=', 'device_inventories.device_id')
->join('device_companies', 'device_companies.id', '=', 'devices.device_company_id');
$active = (clone $baseQuery)->where('device_inventories.status', 'T')
->join('u_devices', 'u_devices.device_inventory_id', '!=', 'device_inventories.id')
->get() ?? null;
$inactive = (clone $baseQuery)->where('device_inventories.status', 'F')->get() ?? null;
return [
'model' => $Self,
'active' => $active,
'inactive' => $inactive,
];
當您使用 時clone,您創建了查詢的副本,因為它是您代碼中的那個點,因此后續使用不會“污染”查詢。

TA貢獻1802條經驗 獲得超10個贊
您需要獲取構建器對象的新實例。$activeInventory
持有你告訴它的所有條件,其中包括所有條件where
。您將需要構建器的副本對其執行不同的查詢:
$something = (clone $activeInventory)->...; $else = (clone $activeInventory)->...;
- 2 回答
- 0 關注
- 220 瀏覽
添加回答
舉報