2017-01-17 21 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'それは2番目のテーブルにあります' propLocation' witch私は ' - > with( 'propLocation')' –

答えて

0

が見つかりません。 whereHas()を使用する必要があります。

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')); 
} 
関連する問題