[英]Eloquent Relationship with eager loading
I have the following code to get the final data $result2
. 我有以下代码来获取最终数据
$result2
。
$agent = auth()->user()->id;
$result = App\AgentStatus->select('callId')->where(function($query)use($agent) {
$query->where('agentId', '=', $agent);
})->first();
$result2 = NULL;
if($result != NULL) {
$id = $result->callId;
$calls = App\CallStatus->where(function($query)use($id) {
$query->where('callId', '=', $id);
})->first();
$id = $calls->callerId;
$result2 = App\Callout->where(function($query)use($id) {
$query->where('number', '=', $id);
})->first();
}
Models: 楷模:
AgentStatus: AgentStatus:
protected $primaryKey = 'agentId';
CallStatus CallStatus
protected $primaryKey = 'callId';
Callout: Primary Key is row id
标注:主键是行
id
This is the first project I'm working on with laravel and I wanted to change the above code to something that implements relationship and eager loading. 这是我正在使用laravel的第一个项目,我想将上面的代码更改为实现关系和急切加载的东西。 For now it works but it might run too slow when tables have a lot more rows.
现在它可以工作但是当表有更多行时它可能会运行得太慢。
Laravel has good documentation Laravel有很好的文档
https://laravel.com/docs/5.8/eloquent-relationships https://laravel.com/docs/5.8/eloquent-relationships
If you have concerns about performance issues you can follow denormalization 如果您对性能问题有疑虑,可以遵循非规范化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.