【发布时间】:2015-06-24 15:16:43
【问题描述】:
我有一个如下所示的查询:
$items = Item::live()
->with('location')
->where('last_location_id', Input::get('last_location_id'))
->get();
这件事的背景是……
2 个表格:物品和汽车。
直播范围是:
public function scopeLive($query)
{
return $query->whereHas('basic_car', function($q)
{
$q->whereNotNull('id')->where('sale_status', 'Live');
});
}
这基本上会检查汽车表是否与项目 'car_id' 字段的 id 匹配,并将在汽车表上运行一些 where 子句。
我现在想检查汽车表上的另一个字段,但使用原始查询中的 Input::get('last_location_id')。
$items = Item::live()
->with('location')
->where('last_location_id', Input::get('last_location_id'))
->orWhere('ROW ON THE CARS TABLE' = Input::get('last_location_id'))
->get();
这个不行,然后我试了一下:
$items = Item::live()
->with('location')
->where('last_location_id', Input::get('last_location_id'))
->orWhere(function($query)
{
$query->where('cars.Location', Input::get('last_location_id'));
})
->get();
这会导致未知列“cars.Location”错误。
我的下一个测试是创建另一个范围:
public function scopeLiveTest($query)
{
return $query->whereHas('basic_car', function($q)
{
$q->whereNotNull('id')->where('sale_status', 'Live')->where('Location', 1); // hardcoded ID
});
}
并用它替换 live() 范围,但我没有得到 orWhere 查询本身的影响,我也无法从输入中指定一个 ID。
我该怎么做?
【问题讨论】:
-
这就是为什么我不太喜欢 Eloquent,因为它比多重连接查询更复杂。如果您决定使用 Fluent,您会发现它变得更加容易
-
你不是第一个这么说的人!到目前为止,我一直在用 Eloquent 玩得很开心,但这是我遇到的第一个问题......
标签: php mysql laravel-4 eloquent where-clause