Aleks Per Aleks Per - 6 months ago 9
PHP Question

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?

Answer

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();
Comments