How to set dynamic filter options

Hi there, I apologize for these silly questions…

I’m adding a relation render on a Model (Visitas), and this Model displays a data table of another Model (Items) with its own filters toolbar … One of these filters gets the options from another Model (Tracking), which has a column that is related to the first Model (Visitas).

Therefore, one could filter “Items” by selecting the “Trackings” assigned to the current “Visita”.

So, I want to show only “Trackings” that belong to the current “Visita” … I found that optionsMethod helps me with that, so I added it and it works if I ‘hardcode’ the Visita Id in the following method:

In theory I can use the following method in the “Tracking” model:

public function getTracking($visitaId)
    {
        return self::where('visita_id', $visitaId)->pluck('name', 'id')->toArray();
        // right? ...
    }

Back in scopes.yaml.

scopes:
    tracking:
        label: Tracking
        modelClass: Autor\Plugin\Models\Tracking
        nameFrom: name
        conditions: tracking_id = :value
        optionsMethod: getTracking

But the question is: how do I pass the Visita’s model Id to it through the Controller? … I haven’t managed to do so! And I’m not sure if that’s the proper way to achieve it either!

Any advise would be greatly appreciated!..

Hmmm, would it be…?

return self::where('visita_id', $this->visita_id)->pluck('name', 'id')->all();

I tried that, but unfortunately visita_id returns null :slightly_frowning_face:

(Is it possible to make a group type of scope tu auto-select by default its first option?)

Hi @danfelbm
it’s not too elegant but you can get the parent model id from the request url.

1 Like

:laughing: that’s what I thought … Indeed, is not too elegant :sweat_smile: but that way it works too. Thank you @danielbidala … I hope to one day become more skilled in OctoberCMS and give back to the community.

Regards.
(if anyone knows any other way that’d be great)