Aleks Per
Aleks Per

Reputation: 1639

Laravel eloquent give wrong result

I have this code:

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'));

}

and this is my database: enter image description here

But when I run it I get this result:

enter image description here

my Auth user id is 8 but I get wrong results? Why?

ALso when i try ; $bids = Auth::user()->bids()->get(); then I get right results///

What is problem?

Upvotes: 0

Views: 698

Answers (2)

Nisam
Nisam

Reputation: 2285

You need to use Advanced Where Clauses

Your Query should like,

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

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5445

you are getting this unexpected error because of orWhere,you can do like this way

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

Upvotes: 2

Related Questions