Php Laravel 4高级版,其中-未知列
我有一个查询,如下所示:Php Laravel 4高级版,其中-未知列,php,mysql,laravel-4,eloquent,where-clause,Php,Mysql,Laravel 4,Eloquent,Where Clause,我有一个查询,如下所示: $items = Item::live() ->with('location') ->where('last_location_id', Input::get('last_location_id')) ->get(); $items = Item::liveAtLocation(Input::get('last_location_id')) ->orWhere(function( $query ) { // to get t
$items = Item::live()
->with('location')
->where('last_location_id', Input::get('last_location_id'))
->get();
$items = Item::liveAtLocation(Input::get('last_location_id'))
->orWhere(function( $query ) { // to get the OR working
$query->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');
});
}
这基本上是检查cars表是否有与items'car_id'字段匹配的id,并在cars表上运行一些where子句
不过,现在我想检查cars表上的另一个字段,但使用原始查询中的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
});
}
用this替换live()作用域是可行的,但是我没有得到查询本身中orWhere的影响,我也不能从输入中指定ID
如何执行此操作?您可以向作用域传递一个参数,如下所示:
$items = Item::live()
->with('location')
->where('last_location_id', Input::get('last_location_id'))
->get();
$items = Item::liveAtLocation(Input::get('last_location_id'))
->orWhere(function( $query ) { // to get the OR working
$query->live()
->with('location')
->where('last_location_id', Input::get('last_location_id'));
})
->get();
至于范围:
public function scopeLiveAtLocation($query, $location_id)
{
return $query->whereHas('basic_car', function($q) use ($location_id)
{
$q->whereNotNull('id')->where('sale_status', 'Live')->where('Location', $location_id);
});
}
这就是为什么对于比多连接查询更复杂的任何查询,我都不喜欢Eloquent。如果你决定使用Fluent,你会发现它变得更容易——你不是第一个这么说的人!到目前为止,我一直在享受Eloquent的乐趣,但这是我遇到的第一个问题。。。