Hello, dear forum users!
I am unable to complete the task:
You need to send a query to the database, given a two-dimensional array.
There is such an array ($activeAttr):
array:2 [▼
"age" => array:2 [▼
0 => "adults"
1 => "children"
]
"color" => array:1 [▼
2 => "black"
]
]
public static function getAvailableAttributes ($categoryTitle, $allAttributes, $activeAttr) {
return static::select($allAttributes)
->where(function ($query) use ($categoryTitle) {
foreach ($categoryTitle as $item) {
$query->orWhere('category_title', $item);
}
})
->where(function ($query) use ($activeAttr) {
foreach ($activeAttr as $item => $content) {
foreach ($content as $id => $attr) {
$query->orWhere($item, $attr);
}
}
})
->get();
}
As a result, I get this request:
select `attribute_name`, `title` from `attributes` where (`attribute_name` = 'age' and `name` = 'adults' or `attribute_name` = 'age' and `name` = 'children' or `attribute_name` = 'color' and `name` = 'black') order by `title` asc
But I need a different type. All received attributes must be separated by "and", and all values of each attribute must be separated by "or"
Something like this:
select `attribute_name`, `title` from `attributes` where (`attribute_name` = 'age' and `name` = 'adults' or `attribute_name` = 'age' and `name` = 'children' ) and ( `attribute_name` = 'color' and `name` = 'black') order by `title` asc
How can I implement this?
Thanks in advance.