Skip to content

Instantly share code, notes, and snippets.

@Muetze42
Last active November 10, 2021 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muetze42/2e51ae903a3ef4719f391eced99e1c43 to your computer and use it in GitHub Desktop.
Save Muetze42/2e51ae903a3ef4719f391eced99e1c43 to your computer and use it in GitHub Desktop.
Laravel Nova: Check if filter is active
<?php // Functions
if (!function_exists('decodeNovaFilter')) {
/**
* Decode the filter string from base64 encoding.
*
* @param string $filtersRequestString
* @return array
*/
function decodeNovaFilter(string $filtersRequestString): array
{
if (empty($filtersRequestString)) {
return [];
}
$filters = json_decode(base64_decode($filtersRequestString), true);
return is_array($filters) ? $filters : [];
}
}
if (!function_exists('isNovaFilterActive')) {
function isNovaFilterActive($model, string $filtersRequest): bool
{
$filters = decodeNovaFilter($filtersRequest);
$check = array_filter($filters, function ($value) use ($model) {
return isset($value['class']) && isset($value['value']) && $value['class'] = $model && $value['value'] != null;
});
return !empty($check);
}
}
?>
<?php // Example Resource
public static function indexQuery(NovaRequest $request, $query): Builder
{
if (!isNovaFilterActive(Status::class, $request->input('filters'))) {
$query->where('status', '!=', static::$model::STATUS_CLOSED);
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment