Refine for Laravel
A package by Hammerstone

Presence Condition

Refine is a paid package. To purchase, head to hammerstone.dev.

The Presence Condition is an extension of the standard Boolean Condition. It is for columns that you want to represent as true or false based on the presence or absence of data.

This condition is usually used against nullable timestamps. Let's look at an example where we're treating the sent_at timestamp column as if it's a boolean.

We'll set up the condition so that when the sent_at column is populated, then the value is true.

// Sent_at is a timestamp...
PresenceCondition::make('sent_at', 'Sent')
// and if there is a value in the column, treat it as `true`.
->trueWhenFilled();

Because we've called trueWhenFilled when the user requests records where [Sent] [is true] the resulting query will be

$query->whereNotNull('sent_at');
Code highlighting powered by torchlight.dev, a Hammerstone product.

and when they request records where [Sent] [is false], the query will be

$query->whereNull('sent_at');

Now we can record the timestamp in the database so we can audit it later, but still give the end user a simple true/false condition.

Configuring Nulls

When using this condition, you must specify whether nulls are true or false. There are a few different ways of doing this.

Because this condition extends the Boolean Condition, you may call either the nullsAreTrue or nullsAreFalse methods that lives in the Boolean Condition.

We have also provided the following methods for the Presence Boolean Condition that may aid in readability:

  • trueWhenNull
  • trueWhenFilled
  • falseWhenNull
  • falseWhenFilled

Clauses

CLAUSE_TRUE

The attribute is null if you have called nullsAreTrue(), trueWhenNull(), or falseWhenFilled() otherwise not null.

CLAUSE_FALSE

The attribute is null if you have called nullsAreFalse(), trueWhenFilled(), or falseWhenNull() otherwise not null.