簡體   English   中英

如何在Laravel中按關系列過濾?

[英]How do I filter by relationship columns in 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是我的第二張表,現在我該如何在其中搜索stats值,應該怎么做?

我嘗試了這個:

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

但它不起作用。

找不到列

您不能像這樣過濾關系列。 您需要使用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'));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM