Answer the question
In order to leave comments, you need to log in
Lazy eager loading and pagination, how to use them all together?
There is some code that, through "eager loading", I load 2 tables from the database ... in short, I can’t explain properly, so I’ll show the code:
General model for loading all data :
$game = Games::getGame($gameName);
if($game){
$game->load('getNewsGame');
}
return $game;
/* Получение новостей игры (upd) */
public function getNewsGame(){
return $this->hasOne(News::class, 'game_news', 'id_games');
}
/* Получение данных об игре */
public function getGame($gameName){
$getCollectionGames = AllGame::getCollectionGames($gameName);
return view('Games.getGame', ['games' => $getCollectionGames]);
}
Answer the question
In order to leave comments, you need to log in
I solved the issue with the following crutch:
Data loading model :
public static function getCollectionGames(string $gameName){
$game = Games::getGame($gameName); // Получаем данные об игре
// "Делаем ленивую нетерпеливую загрузку"
if($game){
$game->load(['getNewsGame' => function($news){
// Добавляем сортировку и даём скрипту вывести на страницу только 10 новостей.
$news->orderBy('id_news', 'desc')->paginate(10);
}]);
}
return $game; // Отдаём эти данные
}
there is already a built-in method for pagination
https://laravel.com/docs/5.5/pagination#basic-usage
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question