Генерируются очень много SQL запросов при отображение страниц.
8 statements were executed 22.93ms
select * from `users` where `id` = 1 limit 1
select * from `books_categories` where `parent_id` = '0'
select * from `books_categories` where `books_categories`.`parent_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33)
select * from `books_categories` where `books_categories`.`parent_id` = 35 and `books_categories`.`parent_id` is not null
select * from `books_categories` where `books_categories`.`parent_id` = 36 and `books_categories`.`parent_id` is not null
select * from `books_categories` where `books_categories`.`parent_id` = 38 and `books_categories`.`parent_id` is not null
select * from `books_categories` where `books_categories`.`parent_id` = 39 and `books_categories`.`parent_id` is not null
select * from `books_categories` where `books_categories`.`parent_id` = 40 and `books_categories`.`parent_id` is not null
***
Контроллере:
public function index()
{
return view('admin.book_categories.index', [
'books_list' => BookCategory::with('children')->parent()->get(),
'delimiter' => '',
'i' => 0
]);
}
В моделе:
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public function scopeParent($query)
{
return $query->where('parent_id', '0');
}
Как оптимизировать код чтобы запросов к базу был максимально оптимально ?