Php 拉维尔';其中不包括';查询困难
我正在尝试运行以下查询并遇到此错误:Php 拉维尔';其中不包括';查询困难,php,laravel,laravel-4,Php,Laravel,Laravel 4,我正在尝试运行以下查询并遇到此错误: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array 当我删除whereNotIn部分时,查询工作。我知道第一个查询是有效的,因为我单独进行了测试。如何修复此错误?代码如下: $alreadyCheckedOutDevicesQuery = DB::connection('NEWSTAFFPORTAL')->table('Device
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
当我删除whereNotIn部分时,查询工作。我知道第一个查询是有效的,因为我单独进行了测试。如何修复此错误?代码如下:
$alreadyCheckedOutDevicesQuery = DB::connection('NEWSTAFFPORTAL')->table('DeviceCheckout_checkout')->select('deviceID')->where('inBy', '=', '')->get();
$alreadyCheckedOutDevices = GlobalModel::convertDBObjectsToArray($alreadyCheckedOutDevicesQuery);
$deviceTableInformation = DB::connection('NEWSTAFFPORTAL')->table('DeviceCheckout_deviceListTestingTable')->select('deviceID', 'name', 'type', 'brand', 'model')->whereNotIn('deviceID', $alreadyCheckedOutDevices)->orderBy('name', 'ASC')->get();
尝试在子查询中执行此操作:
$info = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_deviceListTestingTable')
->select('deviceID', 'name', 'type', 'brand', 'model')
->orderBy('name', 'asc')
->whereNotIn('deviceID', function ($query)
{
$query->from('DeviceCheckout_checkout')
->select('deviceID')
->where('inBy', '');
})
->get();
这将有助于:
$alreadyCheckedOutDevices = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_checkout')
->where('inBy', '=', '')
->lists('deviceID');
$deviceTableInformation = DB::connection('NEWSTAFFPORTAL')
->table('DeviceCheckout_deviceListTestingTable')
->select('deviceID', 'name', 'type', 'brand', 'model')
->whereNotIn('deviceID', $alreadyCheckedOutDevices)
->orderBy('name', 'ASC')
->get();
此外,在性能方面,它应该比使用子查询更好
简化的解释:
+----+--------------------+-----------------+---------+------+-------------+
| id | select_type | type | key | rows | Extra |
+----+--------------------+-----------------+---------+------+-------------+
| 1 | PRIMARY | ALL | NULL | 100 | Using where |
| 2 | DEPENDENT SUBQUERY | unique_subquery | PRIMARY | 1 | Using index |
+----+--------------------+-----------------+---------+------+-------------+
+----+-------------+------+------+------+-------------+
| id | select_type | type | key | rows | Extra |
+----+-------------+------+------+------+-------------+
| 1 | SIMPLE | ALL | NULL | 100 | Using where |
+----+-------------+------+------+------+-------------+
这行得通……我只是不明白为什么这个方法行不通。根据Laravel文档,它应该可以工作。您(或其他人)能解释一下原因吗?因为get()
返回了模型的collection
,然后您将其转换为(我想)数组数组,而您需要ID的平面数组。没错,我将其转换为2D数组。这很有道理,谢谢你。还有,@JosephSilber,谢谢你的回答。