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

Natxlie_In's avatar

Query in Livewire

Hello everyone! to try something new and get out of my comfort zone I decided to try Livewire for my backend.

The problem is that I can't get the Query to give a correct result, probably because I don't know something fundamental and because I got "bad habits" with how simple it is to use Yajra.

In short, I am trying to do a database search on a dataset that must be previously filtered by information that I give it. But 1) either it doesn't work right, or 2) when using the search I get back a complete unfiltered collection using the WHERE I left.

This is the code that works fine until I add something to the Search field, from there it returns something similar to a Model::all().

public $perPage = 100; public $search = ''; public $orderBy = 'id'; public $orderAsc = true;

public function render(){ $search = $this->search; return view('livewire.errors-table',[ 'collection' => CollectionModel::where('foreign_key',Auth::user()->key) ->where('success',0) ->when($search, function ($query, $search) { $query->where('id',$search) ->orWhere('fo','like','%'.$search.'%') ->orWhere('ob','like','%'.$search.'%') ->orWhere('ar','like','%'.$search.'%'); }) ->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc') ->simplePaginate($this->perPage), ]); ]); } } Thank you

0 likes
1 reply

Please or to participate in this conversation.