Skip to content

Instantly share code, notes, and snippets.

@fridzema
Created March 10, 2020 14:54
Show Gist options
  • Save fridzema/eaf96d9d1e05995700d86a1bc5191b67 to your computer and use it in GitHub Desktop.
Save fridzema/eaf96d9d1e05995700d86a1bc5191b67 to your computer and use it in GitHub Desktop.
public function generateRelationsQuery($request, $db_query, $model, $filter_relations)
{
$filter_appends = [];
foreach ($filter_relations as $key => $value) {
if (!isset($value['custom_query'])) {
if ($request->has('filter-remote-' . strtolower($key))) {
$select_value = $request->input('filter-remote-' . strtolower($key));
if (!empty($select_value)) {
$filter_appends['filter-remote-' . strtolower($key)] = $select_value;
if (false != strpos($value['field'], '.')) {
$splitted_filter_values = explode(',', $select_value);
$values = explode('.', $value['field']);
$db_query->whereHas($values[0], function ($db_query) use ($model, $values, $splitted_filter_values) {
$db_query->where(function ($db_query) use ($model, $values, $splitted_filter_values) {
$db_query->whereIntegerInRaw($values[1], $splitted_filter_values);
});
});
} else {
$db_query->whereIntegerInRaw($model . '.' . $value['field'], explode(',', $select_value));
}
}
}
}
}
return $filter_appends;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment