我需要获取数据,而不是在另一个表中,但是我的工作没有...
这是我的第一个表格查询
$hospDriver=DB::table('hospital_drivers')
->select('DriverID')
->get();
这是为了notin
$checkDriverAvail = DB::table('emergencyorders_temp')
->select('DriverID')
->get();
这是主要的
$driverwithin = Driver::
select(array(
'drivers.DriverID',
'drivers.DriverName',
'drivers.DriverPhone',
'ambulance_types.TypeName',
'ambulances.PoliceNo',
'driverlocation.Latitude',
'driverlocation.Longitude',
'driverlocation.DriverActive',
DB::raw($haversine . ' as distance')))
->leftjoin('driverlocation','driverlocation.DriverID','=','drivers.DriverID')
->leftjoin('ambulances','ambulances.AmbulanceID','=','driverlocation.AmbulanceID')
->leftjoin('ambulance_types','ambulances.AmbulanceTypeID','=','ambulance_types.AmbulanceTypeID')
->leftjoin('ambulance_drivers','ambulance_drivers.DriverID','=','drivers.DriverID')
->where('driverlocation.DriverActive', 1)
->whereIn('drivers.DriverID',array([$hospDriver]))
->whereNotIn('drivers.DriverID', array([$checkDriverAvail]))
->having('distance', '<=', 10)
->orderBy('distance', 'ASC')
->get();
这是我的错误
(1/1) ErrorException
Object of class stdClass could not be converted to string
我错在哪里?谢谢!
答案 0 :(得分:1)
Lavarel Query Builder get
确实返回一个Illuminate\Support\Collection
,其中包含每个结果都是PHP stdClass
对象实例的结果。
所以您的变量$hospDriver
和$checkDriverAvail
是对象。函数whereIn
和whereNotIn
需要一个整数数组(例如->whereIn('id', [1, 2, 3])
)才能正常工作。
您需要通过$hospDriver
和$checkDriverAvail
中的集合进行迭代并创建整数数组。
$hospDriverIds=array();
$checkDriverAvailIds=array();
foreach($hospDriver as $driver)
$hospDriverIds[]=$driver->ID;
foreach($checkDriverAvail as $availDriver)
$checkDriverAvailIds[]=$availDriver->ID;
现在您可以根据您的条件使用这些ID数组。变化
->whereIn('drivers.DriverID',array([$hospDriver]))
->whereNotIn('drivers.DriverID', array([$checkDriverAvail]))`
到
->whereIn('drivers.DriverID', $hospDriverIds)
->whereNotIn('drivers.DriverID', $checkDriverAvailIds)
现在,whereIn
和whereNotIn
中的数组包含实现所需的整数。
更多信息(和详细说明)可在此处找到: https://laravel.com/docs/5.5/queries