[英]Repositories and Better Array Usage with Querying DB
我有一個正在使用的Laravel用戶存儲庫,但是為了避免總是困擾N + 1的問題,我試圖讓所有用戶以及他們在用戶數組中的角色和狀態返回。
正如您現在所看到的,由於數據庫中的任何記錄,我的運行速度是其運行時間的200倍以上。 所以我正在努力減少。
我將如何去做呢?
<?php namespace Backstage\Repositories\Users;
use User;
use Backstage\Repositories\DbRepository;
class DbUserRepository extends DbRepository implements UserRepositoryInterface {
protected $model;
function __construct(User $model)
{
$this->model = $model;
}
}
意見/用戶/諧音/ table.blade.php
<td>{{ $user->id }}</td>
<td>{{ $user->full_name }}</td>
<td>{{ $user->email_address }}</td>
<td>{{ $user->role->name }}</td>
<td>{{ $user->status->name }}</td>
您可以使用急切加載擺脫N + 1問題,請參閱文檔
您沒有包括模型,因此不能百分百確定命名,但是我認為您需要這樣做:
function __construct(User $model)
{
$this->model = $model;
$this->model->load('roles');
}
但是,這總是渴望加載DbUserRepository的角色。 您也只能在正確的方法中調用-> load('roles')。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.