php - 在 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);
}
}
解决方案
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();
推荐阅读
- r - 为什么在这个特定示例中 sapply 比 for 循环慢
- python - 尝试为模型构建数据输入时出现 StopIteration 错误
- arrays - 你将如何分割一个元素给出分区大小的numpy数组?
- sql - 查找没有特定子行的行
- aws-cdk - 使用 CDK 创建 ECR 存储库的最佳方法是什么?
- c# - 如何从 Unity 上的 C# 静态类获取当前文件夹
- sql - Sqlite 检查逗号分隔的数字字符串是否包含数字
- mysql - 从 mySQL 获取 HTML 重数据到 excel
- algorithm - 为什么二分查找 Log 的复杂度是以 2 为底的?
- chart.js - 圆环图未使用 csv 数据 chart.js 呈现