Cameron Scott
Cameron Scott

Reputation: 1306

Filter by a Non-Null Value in Laravel Nova

I'm trying to implement a basic filter using Laravel Nova. The filter looks at the field "onboarded_at," which is a datetime field. If the attribute has a timestamp, they're onboarded, and if not, they aren't.

Here's my filter:

public function apply(Request $request, $query, $value)
{
    return $query->where('onboarded_at', $value);
}

public function options(Request $request)
{
    return [
        'Onboarded' => !NULL, // How would I indicate a non-null value here?
        'Not Onboarded' => NULL,
    ];
}

How would I indicate a non-null value in the options function?

Upvotes: 2

Views: 1446

Answers (1)

levi
levi

Reputation: 25161

Use the $value param to modify the query as desired:

public function apply(Request $request, $query, $value)
{
    if($value) {
        return $query->whewhereNotNull('onboarded_at');
    }
    return $query->whereNull('onboarded_at');
}

public function options(Request $request)
{
    return [
        'Onboarded' => 1,
        'Not Onboarded' => 0,
    ];
}

Upvotes: 2

Related Questions