【问题标题】:Skip() function not working [Laravel 5]Skip() 函数不起作用 [Laravel 5]
【发布时间】:2015-09-23 12:46:45
【问题描述】:

我正在尝试在 Laravel 5 中使用 skip and take 函数...当我只使用 take() 时它会起作用,但如果我添加 skip() 它不会...

这是我的代码:

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;
       }

当我添加skip() 时,它只会返回空白页而不会出错。

【问题讨论】:

    标签: laravel-5 skip take


    【解决方案1】:

    第一:

    你少了一个“>”

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

    结果:

    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;
    }
    

    【讨论】:

    • 会返回错误:Unknown column 'order' in 'where clause'
    • 我从您的原始查询中得到了“订单”列。 Order 也是一个保留关键字,所以看起来你的意思是别的东西:)。
    • 我只需要限制这种关系comments
    • 我现在将它们添加到“with”语句中。请试试这个。
    • 抱歉,我把它们弄混了。他们现在应该是正确的。
    猜你喜欢
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2015-08-02
    • 2017-07-15
    • 2016-05-04
    • 2015-06-03
    • 2016-07-03
    • 2016-01-06
    相关资源
    最近更新 更多