【问题标题】:How to return a collection of models with selected columns and relation eager loaded in Laravel 5.2如何在 Laravel 5.2 中返回具有选定列和关系的模型集合
【发布时间】:2016-08-07 08:03:44
【问题描述】:

问题详情:

我有三个模型

  • 带有idname 字段的Directorate
  • Employeeidname 字段和
  • Telephoneidtelemployee_iddirectorate_iddescriptiontype 字段。 employee_id 可以为空,即数据库中存储有带有employee_id = null 的电话

模型相关如下:

  • 一个员工可能有很多电话
  • 一个董事会,可能有很多电话

    class Directorate extends Model
    {
        public function telephones()
        {
            return $this->hasMany(Telephone::class);
        }
    
        public function employees()
        {
            return $this->hasMany(Employee::class);
        }
    }
    
    
    class Employee extends Model
    {
        public function telephones()
        {
            return $this->hasMany(Telephone::class);
        }
    
        public function directorate()
        {
            return $this->belongTo(Directorate::class);
        }
    }
    
    
    class Telephone extends Model
    {
        public function employee()
        {
            return $this->belongsTo(Employee::class);
        }
    
        public function directorate()
        {
            return $this->belongsTo(Directorate::class);
        }
    }
    

问题:

我想获取属于特定Directorate 的所有Telephone 模型的集合,这些模型具有employee_id = null 并且还急切加载了它们的directorate 关系。此外,从生成的 Telephone 模型集合中,我只需要模型的一些字段,即 idteldescription

尝试

到目前为止,我尝试了以下内容:

我在电话模型中创建了一个查询范围:

public function scopeHaveNoEmployeeId($query)
{
    return $query->where('telephones.employee_id', '=', null);
}

在我的控制器中

$myTelephones = Telephone::with('directorate')

                ->haveNoEmployeeId()

                ->where('directorate_id', $directorateId)

                ->get(['id', 'tel', 'description']);

但是,我收到的是过滤模型的请求字段,没有预先加载关系,例如:

[
{
"id": 79,
"tel": "0648136867",
"directorate": null
},
{
"id": 380,
"tel": "0223796011",
"directorate": null
}
] 

之后我也尝试延迟加载关系,但没有运气。

最后我注意到,如果我请求所有电话模型字段,则关系将在我请求时急切加载。例如:

$myTelephones = Telephone::with('directorate')

                ->haveNoEmployeeId()

                ->where('directorate_id', $directorateId)

                ->get();

那么结果:

[
{
"id": 79,
"tel": "0648136867",
"directorate": {
                "id": 23
                 "name": "Some name"
                }    
},
{
"id": 380,
"tel": "0223796011",
"directorate": {
                "id": 23
                 "name": "Some name"
                }       
}  
] 

【问题讨论】:

    标签: laravel eloquent query-builder eager-loading


    【解决方案1】:

    您的电话与董事会模型没有任何关系。 把它放在你的电话模型中。

    public function directorate()
    {
        return $this->belongsTo(Directorate::class);
    }
    

    【讨论】:

    • 这是一个错字,我的模型中确实存在关系,但问题仍然存在。我相应地更正了我的帖子
    【解决方案2】:

    实际上,在深入研究 Laravel 的细节一段时间后,我注意到我最初的问题是断章取义的,有点愚蠢。我首先急切地加载了一个关系,然后我愚蠢地通过不将其包含在 get() 参数中来过滤掉该关系。我只需要执行以下操作:

    $myTelephones = Telephone::with('directorate')
    
                    ->haveNoEmployeeId()
    
                    ->where('directorate_id', $directorateId)
    
                    ->get(['id', 'tel', 'description', 'directorate']);
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2016-11-01
      • 2016-06-12
      • 2020-09-08
      • 2018-02-12
      相关资源
      最近更新 更多