gpt4 book ai didi

php - laravel wherebetween 关系不起作用

转载 作者:行者123 更新时间:2023-12-03 18:45:36 25 4
gpt4 key购买 nike

我在 mysql 中有两个表,1.Vehicles 和 2.Locations我正在使用 Eloquent 关系来定义车辆表与位置的关系。

这是我的车辆模型的样子。

namespace App;

use Illuminate\Database\Eloquent\Model;
class VehicleModel extends Model {
function locations(){
return $this->hasMany("App\locations", "vehicle_id", "id");
}
}

我正在尝试运行一个查询,以获取两个给定时间之间特定车辆的位置。

这是我正在尝试做的:

App\VehicleModel::find(3)->location->whereBetween('locations.time', $range); 

我期望上面的查询会给我日期之间的位置,当我在没有关系的情况下运行时它会这样做。但是当我在关系中运行时,我遇到了意外错误。

PHP 错误:在第 1 行调用成员函数 whereHas() on null

请帮我看看哪里出错了。

提前致谢。

最佳答案

将其更改为:

App\VehicleModel::find(3)->locations()->whereBetween('time', $range)->get();

关于php - laravel wherebetween 关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com