vendredi 30 juin 2017

Dynamic filter collection laravel

I'm working on a filter that i need to use in my system. I have a database table like this:

id - respondent_id - value
1       10            p1
2       10            Male 
3       10            13-19 year
4       11            p2
5       11            female
6       11            20-29 year      
7       12            p2
8       12            Male 
10      13            13-19 year    
11      13            p2
11      13            female
11      13            13-19 year    

for my filter i need to grab for example al the respondent_id's which have the value p2 and have the age 13-19. This filter is dynamic so a other time you can say i need p1 / male / 13-19 year.

How can i reach that when i filter on this for example:

Filter: p2 / female / 13-19 year

that is get respondent 11 and 13 and not 10 and 12

so far i tried this:

 foreach($root[0] as $respondent) {
                $output = $root[0]->filter(function ($value) use ($root, $respondent, $status) {
                    for ($x = 1; $x < count($root); $x++) {
                        foreach ($root[$x] as $respondent) {
                            if ($value === $respondent) {
                                $status = true;
                            }
                        }
                    }

                    return $status;
                })->values()->all();
            }

Here $root stands for a collection with multiple collections which hold the values for each filter. And from there i tried to compare the first collection with the other collections to see which respondent_id they have in common. at this stage it always skips 1 filter.

Any suggestions how to do this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire