2016-12-28 6 views
0

は、私はこのようにそれらをHTMLテーブルでそれらを表示し、ページ付けするカテゴリー(子供)をロードしようとしています:は私のコードでN + 1を引き起こす現在の例ですか?

[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"} 
:私はこのような最初のページのためのSQLクエリーログに記録されている

// 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')); 
    } 

私の質問:はこのコード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')); 
} 
関連する問題