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

rfb's avatar
Level 1

multiple where query

Hi, I'm new to laravel and have only been programming for 6 months. I've been able to make some great progress thanks to Laracast but I've run into an issue that I can not seem to resolve. So I'm hoping one of you can tell me why this is not returning the expected result

Within my livewire dashboard class I'm calling the FilterAds method in my Ad Model. This method filters the model based on a status. When I don't do a search everything works as expected I get items with a specific $adStatus but when I do a search I never get a result. What blows my mind is that if I try ('status_id','=',2) in my query the search works fine and searches within items with a status of 2.

class Dashboard extends Component
{
    use WithPagination; //call pagination on render view
    //Default vars
    protected $pageName = 'Dashboard';
    protected $adStatus = 1;
    //Search
    public $search ='';
    // render the page
    public function render(Request $request)
    {
        $this->setpageattributes($request->path());
        return view('livewire.dashboard', [
            'ads' => Ad::FilterAds('name', $this->search, $this->adStatus),
            'pageName' => $this->pageName,
        ]);
    }
} 

 public static function FilterAds($field, $string, $status)
    {
        return Ad::where([
            ['status_id','=',$status],
            [$field, 'like', '%' . $string . '%'],
        ])->paginate(10);
//also tried this syntax with the same result
//return Ad::where('status_id','=',$status)->where($field, 'like', '%' . $string . '%')->paginate(10);
    }
0 likes
19 replies
Sinnbeck's avatar

Can you please start by formatting your code by adding ``` on a line before and after it? :)

rfb's avatar
Level 1

@Tray2 Thanks, I'll definitely give this a try. But I would also like to understand why this is not working returning the expected results.

Sinnbeck's avatar

Why is this set as static? Any livewire specific thing I am unaware of.

protected static $pageName = 'Dashboard';
protected static $adStatus = 1;
rfb's avatar
Level 1

@Sinnbeck indeed,, those don't need to be static, I've updated the code. However that does not solve the problem.

What I can't get my head round is that my filterads method always returns results with $status = 1. When I dd($status) in my filterads method I do get the correct value for $status but in my query does not seem to use it.

Sinnbeck's avatar

@rfb Ok just trying to understand. You dd inside the FilterAds and $status is 2? But it gets no records? Did you check what queries are being run using debugbar?

rfb's avatar
Level 1

@Sinnbeck indeed, but I get results with $status is 1. i'm not familiar with debugbar, but I'll give it a try. Thanks :D

webrobert's avatar
Level 51

a bit convoluted there. Look...

use WithPagination;

public $pageName = 'Dashboard';
public $status = 1;
public $search;

public function getAdsProperty()
{
    return Ad::query()
     ->where('status_id', $this->status)
     ->where($this->field, 'like', '%' . $this->search. '%')
     ->paginate(10);
}

public function render()
{
    return view('livewire.dashboard');
}

then in your view just type and the search will filter. And change the status $set('status', 3) and the status will change. easy peasy. one small change $ads now needs to be $this->ads in the blade.

one issue $this->setpageattributes($request->path()) I assume isn't working properly UNLESS you already realize that the request path changes to a different route on the second render.

rfb's avatar
Level 1

@webrobert I only sent a part of my livewire dashboard class so that is why the code probably looks a bit weird :D

I've created multiple routes to Dashboard:class. Within that class I have a method setpageattributes that looks up some attributes for the page so that I can use the same class for each of the different views. These pages basically show the ads based on their status (active, archive, due). This worked fine until I introduced the search.

I'm fairly new to this so there is probably a better way but I'm just trying to understand why this does not work.

But I think this remark points out the issue: $this->setpageattributes($request->path()) I assume isn't working properly UNLESS you already realize that the request path changes to a different route on the second render.

It seems on the second render the default value is always used. If I change $adstatus to another value I get those results, so it seems that on the second render $adstatus is not set by setpageattributes. However, if I look at the URI there is no change on the second render.

    Route::get('/', Dashboard::class)->name('dashboard');
    Route::get('/dashboard', Dashboard::class)->name('dashboard');
    Route::get('/archive', Dashboard::class )->name('archive');
    Route::get('/due', Dashboard::class )->name('due');


class Dashboard extends Component
{
    use WithPagination; //call pagination on render view

    //Default vars
    protected $pageName = '';
    protected $adStatus = 1;
    protected static $adSelector = array(
        'dashboard' => array ('Route'=> 'dashboard', 'PageName' => 'Dashboard', 'Status'=> 1 ),
        'archive' => array ('Route'=> 'archive', 'PageName' => 'Archive', 'Status'=> 2 ),
        'due' => array ('Route'=> 'due', 'PageName' => 'Due', 'Status'=> 3 ),
    );

    //Search
    public $search ='';

     //Look for dashboard route and filter status and set PageName
    protected function setpageattributes($request)
    {
        //define adStatus and Pagename if the page $request exists in the array
        if (array_key_exists($request, self::$adSelector)){
            $this->adStatus = self::$adSelector[$request]['Status'];
            $this->pageName = self::$adSelector[$request]['PageName'];
        }
    }
    // render the page
    public function render(Request $request)
    {
        $this->setpageattributes($request->path());
        return view('livewire.dashboard', [
            'ads' => Ad::FilterAds('name', $this->search, $this->adStatus),
            'pageName' => $this->pageName,
        ]);
    }
}
rfb's avatar
Level 1

@webrobert You and @sinnbeck put me on the right track, using debugbar I see that the path changes on the second render from /archive to /livewire/message/dashboard

So I'll have to rethink how to do this.

PS awesome reply speed on this forum :D

webrobert's avatar

@rfb make a dashboard livewire component that you extend

    Route::get('/dashboard', Dashboard::class)->name('dashboard');
    Route::get('/archive', ArchiveDashboard::class )->name('archive');
    Route::get('/due', DueDashboard::class )->name('due');

or set those things on mount as a public properties. so they persist and dont change with each request.

rfb's avatar
Level 1

@webrobert thanks, I'll look into that

just wondering, is there a way to pass this as a parameter in the route? This is incorrect but something like

Route::get('/dashboard', Dashboard::class(status=1))->name('dashboard');
Route::get('/archive', Dashboard::class(status=2))->name('dashboard');
webrobert's avatar

@rfb,

then you loose the naming...

Route::get('/dashboard/{view}', Dashboard::class)->name('dashboard');

then you capture it in the component

public $view;
webrobert's avatar

this does have an added benefit though.. you can still pass the $view into the component

<livewire:dashboard :view="$lookAtMe" />

not that you'd do that with your dashboard. but you get the idea.

rfb's avatar
Level 1

@webrobert Thanks, the naming isn't vital so I think this is the way I'll move forward!

webrobert's avatar

@rfb, cool. if you got what you needed. please close the thread by marking a best reply.

rfb's avatar
Level 1

@webrobert I'll mark your reply with

one issue $this->setpageattributes($request->path()) I assume isn't working properly UNLESS you already realize that the request path changes to a different route on the second render.

as best reply as it set me on the path to finding the issue and made me learn the most.

But would have to send my thanks to @sinnbeck for pointing out debugbar since this will help me greatly in debugging future issues

Thanks guys !

1 like

Please or to participate in this conversation.