簡體   English   中英

雄心勃勃的關系與渴望加載

[英]Eloquent Relationship with eager loading

我有以下代碼來獲取最終數據$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();
}

楷模:

AgentStatus:

protected $primaryKey = 'agentId';

CallStatus

protected $primaryKey = 'callId';

標注:主鍵是行id

這是我正在使用laravel的第一個項目,我想將上面的代碼更改為實現關系和急切加載的東西。 現在它可以工作但是當表有更多行時它可能會運行得太慢。

Laravel有很好的文檔

https://laravel.com/docs/5.8/eloquent-relationships

如果您對性能問題有疑慮,可以遵循非規范化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM