在我的应用程序中,有一种情况我必须运行相同的查询 3 次,但每个查询都有一些不同的条件。例如:active:查询 + active conditions,inactive:查询 + inactive conditions。ETC。
这是我的代码:
$activeInventory = $inactiveInventory = \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 records
$active = $activeInventory
->where('device_inventories.status', 'T')
->join('u_devices', 'u_devices.device_inventory_id', '!=', 'device_inventories.id')
->get() ?? null;
// inactive records
$inactive = $inactiveInventory
->where('device_inventories.status', 'F')
->get() ?? null;
// returning data
return [
'model' => $Self,
'active' => $active,
'inactive' => $inactive,
];
u_devices请注意,我已在查询中加入表Active。但是当我运行Inactive查询时,join withu_devices也出现在该查询中。即使我使用不同的变量来存储基本查询并运行它。
我在这里做错了什么..?
MM们
守候你守候我