首页 > 解决方案 > 在 Laravel Eloquent 方法中,我的关系数组中没有收集任何数据

问题描述

我在控制器中使用 eloquent 方法从数据库中收集数据,但发生了一些奇怪的事情。如果我在下面使用此代码,

$female_old_visitors = Treatment::with('diseases', 'patient', 'insurance', 'referer')
            ->leftJoin('patients', 'treatments.patient_id', 'patients.id')
            ->where('treatments.visit_status', 'old')
            ->where('patients.gender', 'female')
            ->whereBetween('treatments.date', $date_range)
            ->get();

我可以获得所有我想要的数据,包括疾病和推荐人

    Collection {#3053 ▼
  #items: array:25 [▼
    0 => Treatment {#2799 ▼
      ...
      #relations: array:4 [▼
        "diseases" => Collection {#3346 ▼
          #dates: array:1 [▶]
          #cascadeDeletes: array:1 [▶]
          #guarded: []
          #connection: "mysql"
          #table: "diseases"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:32 [▶]
          #original: array:32 [▶]
          #changes: []
          #casts: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
          #forceDeleting: false
        }
        "patient" => Patient {#3328 ▶}
        "insurance" => Insurance {#3346 ▶}
        "referer" => TreatmentReferer {#3138 ▶}
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #forceDeleting: false
    }

但是当我使用这样的其他代码时

$common_insurance_old_visitors = Treatment::with('diseases', 'patient', 'insurance', 'referer')
        ->leftJoin('insurances', 'treatments.insurance_id', 'insurances.id')
        ->where('treatments.visit_status', 'old')
        ->where('insurances.id', 1)
        ->whereBetween('treatments.date', $date_range)
        ->get();

除疾病和推荐人外,所有数据均已选择或收集

    Collection {#3053 ▼
  #items: array:25 [▼
    0 => Treatment {#2799 ▼
      ...
      #relations: array:4 [▼
        "diseases" => Collection {#3246 ▼
          #items: []
        }
        "patient" => Patient {#3328 ▶}
        "insurance" => Insurance {#3346 ▶}
        "referer" => null
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #forceDeleting: false
    }

我一直在检查我的数据库,数据仍然存在并且该列不为空,应该像我首先使用的代码一样收集它。是因为我错误地使用了左连接吗?我还是 laravel 的新手,感谢任何给我解决这个问题的人

这是我的治疗模型

    <?php

namespace App;

    use Carbon\Carbon;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Iatstuti\Database\Support\CascadeSoftDeletes;

class Treatment extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $dates = ['deleted_at'];
    protected $cascadeDeletes = ['medicines', 'actions', 'referer', 'diseases', 'queues'];
    protected $guarded = [];

    public function queues(){
        return $this->hasMany(TreatmentQueue::class);
    }

    public function treatmentType(){
        return $this->belongsTo(TreatmentType::class);
    }

    public function medicines(){
        return $this->hasMany(TreatmentMedicine::class)->with('medicine', 'recu');
    }

    public function actions(){
        return $this->hasMany(TreatmentAction::class)->with('action', 'doctor', 'nurse', 'recu', 'teeth', 'therapy', 'treatmentDisease');
    }

    public function insurance(){
        return $this->belongsTo(Insurance::class);
    }

    public function referer(){
        return $this->hasOne(TreatmentReferer::class)->with('puskesmas', 'disease');
    }

    public function diseases(){
        return $this->hasMany(TreatmentDisease::class)->with('disease', 'doctor', 'teeth');
    }

    public function patient(){
        return $this->belongsTo(Patient::class);
    }


}

标签: phplaravelcollectionseloquenteager-loading

解决方案


with()用于急切加载。这基本上意味着,沿着主模型,Laravel 将预加载您指定的关系。如果您有一组模型并且想要为所有模型加载关系,这将特别有用。因为通过预先加载,您只需运行一个额外的数据库查询,而不是为集合中的每个模型运行一个。

在你的例子中。

你为什么使用左连接?它已经包含在with()你使用with('patient')它意味着你加入treatments加入patient

$female_old_visitors = Treatment::with('diseases', 'insurance', 'referer')
                        ->with(['patient' => function ($q) {
                                $q->where('gender', 'female');
                            }])
                            ->where('visit_status', 'old')
                            ->whereBetween('treatments.date', $date_range)
                            ->get();

急切加载


推荐阅读