php - 在 Laravel 中计算后从 mysql 表检索 id 时出现问题

标签 php mysql laravel

我有一个有效的距离计算查询,即它返回最近的点,但它只是产生一个距离数组。我想要做的是检索存储在数组中的点的 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/

相关文章:

php - 流明 : How to fix the SQLSTATE[42000]: Syntax error or access violation: 1071 on laravel lumen

php - 500 Internal Server Error-所有Ajax调用,PHP

javascript - 未捕获的安全错误 : Failed to read the 'contentDocument' property from 'HTMLIFrameElement' : Blocked a frame

php - 为什么在 PHP 框架中使用依赖注入(inject)组件

php - 仅当php中没有 "%"登录时如何显示表单元素

mysql innodb 每个表一个文件,每个文件位于不同的目录中?

MYSQL查询获取Group BY中的SUM和LAST记录值

php - laravel 在插入一行后无法检索 ID

javascript - 绑定(bind)的输入值未出现在请求中

php - 页面加载时重新加载下拉菜单