Search code examples
laravel-5skiptake

Skip() function not working [Laravel 5]


I am trying to use skip and take function in Laravel 5... When I just use take() it will work find but if I add skip() it will not...

Here is my code:

public function orderWhat($what){
           $this->what = $what;
           //QUERY FOR GROUPS AND PRODJECT_GROUP
           $userCondition = function($q){
                        $q->where('user_id',Auth::id())->where('project_id',$this->id)->skip(20)->take(20);
                    };

           //QUERY FOR COMMENTS AND PROJECT_COMMENT     
           $commentsCondition = function($q){
                        $q->where('project_id',$this->id)->where('order',$this->what)->orderBy('comments.id', 'DESC')-skip(20)->take(20);
                    };

           //RETURN PROJECT WITH COMMENTS        
           $results = Project::with(['comments' => $commentsCondition,'groups' => $userCondition])
                                ->whereHas('groups', $userCondition)
                                ->whereHas('comments', $commentsCondition)

                                ->get();
           return $results;
       }

When I add skip() it just return blank page without errors.


Solution

  • First:

    You are missing a ">"

    $q->where('project_id',$this->id)->where('order',$this->what)->orderBy('comments.id', 'DESC')-skip(20)->take(20);
    

    Result:

    public function orderWhat($what){
      $this->what = $what;
    
      $result = Project::with(['comments' => function($q) {
        $q->where('project_id',$this->id)->where('order',$this->what)
          ->skip(20)->take(20);
      }, 'groups' => function($q) {
        $q->where('user_id',Auth::id())->where('project_id',$this->id)->skip(20)->take(20);
      }])->get();
    
       return $results;
    }