【问题标题】:Searching from multiple related table using laravel 5 Eager Loading使用 laravel 5 Eager Loading 从多个相关表中搜索
【发布时间】:2015-04-11 18:03:38
【问题描述】:

实际上我有 4 个相关模型并使用标准从两个表中搜索数据。如果我忽略从address 模型搜索,使用以下查询我会得到结果。但我需要从PropertyAddress 模型中搜索,其中显示地址表列的错误。

        $min_price  = !empty($request['min_price']) ? $request['min_price'] : 500;
        $max_price  = !empty($request['max_price']) ? $request['max_price'] : 50000000000;
        $arrCriteria = [
                'properties.status'            => 1,
                'properties.category'          => $request['search_category'],
                'properties.type'              => $request['contract'],
                'addresses.city'               => $request['search_city'], //show error
                'addresses.area'               => $request['property_area'],  //show error
                'properties.bed_room'          => $request['search_bedroom'],
                'properties.bath_room'         => $request['bath_room'],
        ];
        $property = Property::with('address', 'photo', 'user')
                        ->where($arrCriteria)
                        ->whereBetween('properties.price', [$min_price, $max_price])
                        ->get();

【问题讨论】:

    标签: php laravel laravel-4 eloquent laravel-5


    【解决方案1】:

    您遇到的错误是由于您的属性表没有addresses.city和addresses.area列,为了同时过滤和预先加载您的关系,您必须编写如下内容

    $arrCriteria = [ 'properties.status' => 1, 'properties.category' => $request['search_category'], 'properties.type' => $request['contract'], 'properties.bed_room' => $request['search_bedroom'], 'properties.bath_room' => $request['bath_room'], ];

    $addressCriteria = [
    'addresses.city' => $request['search_city'],
    'addresses.area' => $request['property_area'],
    ];
    

    最后

    $property = Property::with(['address' => function ($query) use ($addressCriteria) {
        return $query->where($addressCriteria);
    }, 'photo', 'user'])
        ->where($arrCriteria)
        ->whereBetween('properties.price', [$min_price, $max_price])
        ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2017-06-28
      • 2016-05-29
      • 1970-01-01
      • 2017-09-17
      • 2017-05-23
      相关资源
      最近更新 更多