Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david19's avatar

Query Builder question

Hello Team, I have this query which work fine.

public function render()
    {


        $mypasswords = MyPassword::query($this->search)
        ->select('id','title','user_name','url','notes','password','user_id')
        ->orWhere('id','like','%'.$this->search.'%')
        ->orWhere('title','like','%'.$this->search.'%')
        ->orWhere('url','like','%'.$this->search.'%')
        ->orWhere('user_name','like','%'.$this->search.'%')
        ->orWhere('notes','like','%'.$this->search.'%')
        ->orderBy($this->orderBy,$this->sort)
        ->paginate($this->perPage);

        return view('livewire.my-password-datatable',[
            'mypasswords'  => $mypasswords,
        ]);
    }

Now i will add only the records for the specific user.

->where('user_id','=',auth()->id())

But i can not add to this query, its not working.

$mypasswords = MyPassword::query($this->search)
        ->select('id','title','user_name','url','notes','password','user_id')
	    ->where('user_id','=',auth()->id())
        ->orWhere('id','like','%'.$this->search.'%')
        ->orWhere('title','like','%'.$this->search.'%')
        ->orWhere('url','like','%'.$this->search.'%')
        ->orWhere('user_name','like','%'.$this->search.'%')
        ->orWhere('notes','like','%'.$this->search.'%')
        ->orderBy($this->orderBy,$this->sort)
        ->paginate($this->perPage);
0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to nest it. https://laravel.com/docs/9.x/queries#logical-grouping

        $mypasswords = MyPassword::query($this->search)
        ->select('id','title','user_name','url','notes','password','user_id')
        ->where('user_id','=',auth()->id())
       ->where(function($query) {
              $query->orWhere('id','like','%'.$this->search.'%')
                 ->orWhere('title','like','%'.$this->search.'%')
                 ->orWhere('url','like','%'.$this->search.'%')
                 ->orWhere('user_name','like','%'.$this->search.'%')
                 ->orWhere('notes','like','%'.$this->search.'%');
        })
        ->orderBy($this->orderBy,$this->sort)
        ->paginate($this->perPage);
1 like

Please or to participate in this conversation.