Laravel: 不同条件的查询构建器基础查询
Laravel: Query builder base query for different conditions
在我的应用程序中,有一种情况我必须 运行 同一个查询 3 次,但每个查询都有一些不同的条件。例如:active:
查询+active conditions
,inactive:
查询+inactive conditions
。等等
这是我的代码:
$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,
];
请注意,我在 Active
查询中加入了 u_devices
table。但是当我 运行ning 时,与 u_devices
连接的 Inactive
查询也出现在该查询中。即使我使用不同的变量来存储基本查询并 运行ning 它。
我在这里做错了什么..?
这是由于追加查询逻辑的工作方式所致。当您将更改附加到查询的一个版本时,它会修改原始版本,因此来自基础的任何后续查询也会受到影响。您应该能够使它与 PHP:
中的 clone
关键字一起使用
$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
时,您会在代码中创建查询的副本,因此后续使用不会 "pollute" 查询。
您需要获取构建器对象的新实例。 $activeInventory
持有您告诉它的所有条件,其中包括所有 where
条件。您将需要构建器的副本对其执行不同的查询:
$something = (clone $activeInventory)->...;
$else = (clone $activeInventory)->...;
在我的应用程序中,有一种情况我必须 运行 同一个查询 3 次,但每个查询都有一些不同的条件。例如:active:
查询+active conditions
,inactive:
查询+inactive conditions
。等等
这是我的代码:
$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,
];
请注意,我在 Active
查询中加入了 u_devices
table。但是当我 运行ning 时,与 u_devices
连接的 Inactive
查询也出现在该查询中。即使我使用不同的变量来存储基本查询并 运行ning 它。
我在这里做错了什么..?
这是由于追加查询逻辑的工作方式所致。当您将更改附加到查询的一个版本时,它会修改原始版本,因此来自基础的任何后续查询也会受到影响。您应该能够使它与 PHP:
中的clone
关键字一起使用
$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
时,您会在代码中创建查询的副本,因此后续使用不会 "pollute" 查询。
您需要获取构建器对象的新实例。 $activeInventory
持有您告诉它的所有条件,其中包括所有 where
条件。您将需要构建器的副本对其执行不同的查询:
$something = (clone $activeInventory)->...;
$else = (clone $activeInventory)->...;