繁体   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