【问题标题】:where Has Condition on a hasMany relationship in Laravel 5.8Laravel 5.8 中 hasMany 关系的条件
【发布时间】:2020-05-03 06:10:52
【问题描述】:

我在一个模型 FeeModuleModel 中有如下关系

   public function heads()
   {
       return $this->hasMany('App\Models\FeeHeadModel','location_id','id');
   }

在我的控制器文件中,我只需要获取 FeeHeadModel 类型为非结构化的值 FeeModuleModel 我的控制器代码如下所示

$modules = FeeModuleModel::where('vt_ay_id', '=', Session::get('sess_ay_id'))->with(['heads'=>function($q){ 
            $q->where('type','=','unstructured');
        }])->orderby('priority', 'asc')->get();

这会失败并出现以下错误

调用数组上的成员函数 getRelationExistenceQuery()

我的代码有什么问题以及我可以做些什么来解决它

【问题讨论】:

    标签: eloquent laravel-5.8 eloquent-relationship php-7.3 wherehas


    【解决方案1】:

    请将您的代码更改为这个

    $modules = FeeModuleModel::with(['heads'=>function($q){ 
                $q->where('type','=','unstructured');
            }])->where('vt_ay_id', '=', Session::get('sess_ay_id'))->orderby('priority', 'asc')->get();
    

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2021-12-14
      • 2020-03-25
      相关资源
      最近更新 更多