2016-12-28 8 views
0

я пытаюсь загрузить Категория (детей), чтобы просмотреть их в HTML таблице и постраничной их так:- текущий пример вызывает N + 1 в моем коде?

// show category children's 

public function show(subCategory $sections) 
    { 
     // Eager Loading the relationship 
     $sections->with('chlidrens'); 

     // paginate the category - childrens 

     $result = $sections->chlidrens()->orderBy('created_at','desc')->paginate(5); 

     return view('CompanySections.show',compact('sections','result')); 
    } 

Я вошел SQL-запрос для первой страницы, как это:

[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `catnodestreetable` where `id` = ? limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `customers` where `customers`.`id` = ? limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select count(*) as aggregate from `catnodestreetable` where `catnodestreetable`.`node_parent_id` = ? and `catnodestreetable`.`node_parent_id` is not null"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `catnodestreetable` where `catnodestreetable`.`node_parent_id` = ? and `catnodestreetable`.`node_parent_id` is not null order by `created_at` desc limit 5 offset 0"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `categories` where `categories`.`id` = ? limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `clothes` where `clothes`.`item_id` = ? and `clothes`.`item_id` is not null limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `clothes` where `clothes`.`item_id` = ? and `clothes`.`item_id` is not null limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `clothes` where `clothes`.`item_id` = ? and `clothes`.`item_id` is not null limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `clothes` where `clothes`.`item_id` = ? and `clothes`.`item_id` is not null limit 1"} 
[2016-12-28 08:19:20] local.INFO: query {"query":"select * from `clothes` where `clothes`.`item_id` = ? and `clothes`.`item_id` is not null limit 1"} 

Мой вопрос: Этот код вызывает N + 1 !! и если он сделал ... как это исправить ??

ответ

0

with звонок, который вы делаете, не имеет никакого эффекта. Это не учитывается при непосредственном запуске метода отношений. Что вы можете сделать, это следующее: eager loaded constraints:

public function show(subCategory $sections) 
{ 
    $result = $sections->with(['children' => function($query) { 
     $query->orderBy('created_at', 'desc'); 
    }])->paginate(5); 

    return view('CompanySections.show',compact('sections','result')); 
}