如何做laravel渴望加载多个约束?

时间:2017-08-24 09:56:15

标签: laravel laravel-5.4 eager-loading laravel-eloquent

$institutes = Institute::with(['address' => function($query){
                 $query->whereCityId(Input::get('city_id'));
                 $query->whereIn('area_id',Input::get('area_id'));
              }])->get();

为foreach()提供的参数无效

3 个答案:

答案 0 :(得分:0)

问题可能在这里

Risk <- read_delim("/home/seckindinc/Desktop/Projects/R/Data/Risk.txt", 
                   "\t", escape_double = FALSE, trim_ws = TRUE)

 normallity_plot_func <- function (table_name, column_name) {
   histogram <- hist(table_name$column_name, breaks = 10,  col = "lightgray", xlab = "a")
   xfit <- seq(min(table_name$column_name), max(table_name$column_name), length = 40)
   yfit <- dnorm(xfit, mean = mean(table_name$column_name), sd=sd(table_name$column_name))
   yfit <- yfit * diff(histogram$mids[1:2]) * length(table_name$column_name)
   lines(xfit, yfit, col= "black", lwd= 2)
 }

 normallity_plot_func(Risk,INCOME)

尝试确保$query->whereIn('area_id',Input::get('area_id')); 实际返回一个数组:

Input::get('area_id')

答案 1 :(得分:0)

whereCityId模型的范围?文档中的示例的范围仅与模型链接。

尝试使用列名替换范围并尝试。 ex替换为city_id

 $institutes = Institute::with(['address' => function($query){
                     $query->where('city_id',Input::get('city_id'));
                     $query->whereIn('area_id',Input::get('area_id'));
                  }])->get();

答案 2 :(得分:0)

确实有效但返回

address: null

条件匹配的任何地方。但它会归还所有研究所。要么我们可以在集合上使用过滤器,要么 whereHas

$institutes = Institute::whereHas('address', function ($query) use ($city_id) {
                   $query->whereCityId($city_id);
              })->get();

$institutes = Institute::with(['address' => function ($query) use ($city_id) {
                   $query->whereCityId($city_id);
              }])->get()->filter(function ($institute){ 
                   return $institute->address != null; 
              });