vendredi 5 février 2016

Laravel Eager Loading W/ Pagination

In short Words i have Posts that have many comments so far iam able to paginate posts so user can navigate between page to another page. so every post have many Comments and i don want to load all comments at once i want also to have paginator in comments to have chunk of comments every time user press some button and having total comments count and every thing available in paginator object what so i am doing is like so:

       public function PhotoFeed()
       {
         return PhotoFeed::with('comments')
                  ->whereIn('user_id',$this->getFrom())
                  ->simplePaginate(5);

        }

and in PhotoFeed model:

        public function comments()
        {
         return $this->hasMany(Comment::class ,'feed_id');
        }

but i want able to add paginator to comment relation Like So

        public function comments()
        {
         return $this->hasMany(Comment::class ,'feed_id')->paginate(2);
        }

because i have know later that relation function must return object of type relation

I have also Tried this solution and it works

         protected $appends = ['commentspaginator'];

         public function getCommentsPaginatorAttribute()
         {
         return 
           $this->hasMany(Comment::class ,'feed_id')->with('user')->paginate(3);

          }

but the problem it break the eager loading thing so every post have to do it own query so likely it is not good for performance

and Ideas??



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SOxtJy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire