laravel之间的关系不起作用 [英] laravel wherebetween in relations not working

查看:100
本文介绍了laravel之间的关系不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mysql中有两个表,分别是1. Vehicles和2.Locations
i使用雄辩的关系来定义Vehicle Table与位置的关系。

I have two tables in mysql, 1. Vehicles and 2.Locations i am using eloquent relations to define relation of the vehicle table to locations.

namespace App;

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

我正在尝试运行可以使我得到查询的查询

i am trying to run a query that can get me locations of specific vehicle between two given times.

这是我要执行的操作:

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

我期望上面的查询将为我提供日期之间的位置,这是我在没有关系的情况下运行的日期。但是当我确实在关系中运行时,出现了无法处理的错误。

I was expecting to above query would give me locations between dates which it does when i run without the relation. but when when i do run in a relation i am getting an unexpeted error.

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

请帮助我看看我要去哪里。

Please help me see where i am going wrong.

请先谢谢您。

推荐答案

将其更改为:

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

这篇关于laravel之间的关系不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆