Last active
November 10, 2021 09:52
-
-
Save Muetze42/2e51ae903a3ef4719f391eced99e1c43 to your computer and use it in GitHub Desktop.
Laravel Nova: Check if filter is active
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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