我有一个有效的距离计算查询,即它返回最近的点,但它只是产生一个距离数组。我想要做的是检索存储在数组中的点的 ID,以便我可以访问更多数据。
这是查询:
$Eastings = $asset->Eastings;
$Northings = $asset->Northings;
$micromarket_size = 10000;
$competitors = DB::table('homes')
-> select(DB::raw('SQRT(POW('.$Eastings.' - `Eastings`,2) + POW('.$Northings.' - `Northings`,2)) AS distance'))
-> having('distance', '<', $micromarket_size)
-> get();
并尝试像这样访问 id:
$competitors->id;
产生错误 trying to get property of non-object
最佳答案
您的选择查询未请求 id 字段:
->select(array('id', DB::raw('SQRT(POW('.$Eastings.' - `Eastings`,2) + POW('.$Northings.' - `Northings`,2)) AS distance')))
关于php - 在 Laravel 中计算后从 mysql 表检索 id 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23116384/