[英]Laravel 4 Advanced Where - Unknown Column
我有一個查詢,看起來像這樣:
$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');
});
}
基本上,這會檢查cars表中是否有與項目'car_id'字段匹配的id,並將在cars表中運行一些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的影響,而且我也無法從Input中指定ID。
我怎樣才能做到這一點?
您可以像這樣將參數傳遞給范圍:
$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);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.