【问题标题】:Laravel eloquent give wrong resultLaravel 雄辩给出错误的结果
【发布时间】:2016-05-31 22:27:46
【问题描述】:

我有这个代码:

public function inbox()
{
    $id = Auth::user()->id;
    $bids = Bid::where('user_id',$id)
               ->where('status','0')
               ->orWhere('status','4')
               ->latest()->get();
               dd($bids);
    return view('rooms.inbox', compact('bids'));

}

这是我的数据库:

但是当我运行它时,我得到了这个结果:

我的 Auth 用户 ID 是 8,但我得到了错误的结果?为什么?

当我尝试时; $bids = Auth::user()->bids()->get(); 然后我得到正确的结果///

什么问题?

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    由于 orWhere,您会收到此意外错误,您可以这样做

    $bids = Bid::where('user_id',$id)
                           ->where(function ($query) {
                             $query->where('status','0')
                             ->orWhere('status','4');
                           })
                           ->latest()->get();
    

    【讨论】:

      【解决方案2】:

      你需要使用Advanced Where Clauses

      您的查询应该喜欢,

      $bids = Bid::where('user_id',$id)
                  ->where(function ($query) {
                      $query->where('status', '0')
                          ->orWhere('status', '4');
                  })->latest()->get();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-09
        • 2018-04-13
        • 2017-05-16
        • 2017-08-04
        • 1970-01-01
        • 2013-05-07
        相关资源
        最近更新 更多