【问题标题】:No data was collected in my relation array in Laravel Eloquent method在 Laravel Eloquent 方法中,我的关系数组中没有收集数据
【发布时间】:2020-02-09 19:05:32
【问题描述】:

我在控制器中使用 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);
    }


}

【问题讨论】:

    标签: php laravel collections eloquent eager-loading


    【解决方案1】:

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

    在你的例子中。

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

    $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();
    

    eager-loads

    【讨论】:

    • 谢谢,它成功了,但问题出在这里,例如所有记录都是 40、30 男性和 10 女性,如果我使用我的代码获取女性,我可以得到 10,但有疾病和推荐人关系问题,但如果我使用你的代码,疾病和引用问题得到解决,但我得到 40 条记录而不是 10
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2021-12-10
    • 2015-12-07
    • 2019-12-11
    相关资源
    最近更新 更多