2017-01-17 5 views
0

У меня есть этот запросКак фильтровать по столбцам отношений в Laravel?

public function index() 
{ 
    $properties = PropertyDetail::query()->with('propLocation'); 

    $properties->where('type', Input::get('unitType')); 
    $properties->where('purpose', Input::get('purpose')); 
    $properties->where('active', 1); 

    if (Input::has('specifyType')) { 
     $properties->where('specify_type', Input::get('specifyType')); 
    } 

    if (Input::has('location')) { 
     $properties->where('state', Input::get('location')); 
    } 

    $result = $properties->get(); 
    return View::make('portal.properties.view', compact('result')); 
} 

propLocation мой второй стол теперь, как я могу найти значение статистику там, Как это должно быть сделано?

Я попытался это:

if (Input::has('location')) { 
    $properties->where('state', Input::get('location')); 
} 

, но он не работает.

Колонка не найден

+0

Какой столбец вам не хватает? – GiamPy

+0

'state' он находится во второй таблице' propLocation' witch Я сошёл '-> с ('propLocation')' –

ответ

0

Вы не можете фильтровать отношения столбцов, как это. Вы должны использовать whereHas().

Try так:

public function index() 
{ 
    $properties = PropertyDetail::query()->with('propLocation'); 

    $properties->where('type', Input::get('unitType')); 
    $properties->where('purpose', Input::get('purpose')); 
    $properties->where('active', 1); 

    if (Input::has('specifyType')) { 
     $properties->where('specify_type', Input::get('specifyType')); 
    } 

    if (Input::has('location')) { 
     $properties->whereHas('propLocation', function ($query) { 
      $query->where('state', Input::get('location')); 
     }); 
    } 

    $result = $properties->get(); 
    return View::make('portal.properties.view', compact('result')); 
} 
Смежные вопросы