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

skoobi's avatar
Level 13

Search query on ->when() with relationship (Solved)

Hi.

I'm just trying to get a search query back from 2 dropdowns, one is a relationship of the Module model.

Im using the '->when()' property to get the results if the query is passed through, but i can't figure out how to get it to work on the relationship.

$modules = Module::
    		when($this->org != '', function ($query) {
                return $query->where('organisation_id', $this->org);
            })
            ->when($this->module != '', function ($query) {
                return $query->where('module_steps.module_id', $this->module);
            })
            ->get();

I keep getting 'Property [$modules] not found on component: [modules.steps]'

Cant quite get it right. Any ideas?

0 likes
2 replies
skoobi's avatar
Level 13

Solved...

I had the variable declared as public.

sr57's avatar

$this->module is not defined

Please or to participate in this conversation.