Laravel helper for your own falsy conditions checking

Mohammed Osama
3 min readJul 22, 2019

--

You probably have written a condition that returns false, then you always keep prefixing it with the “!” inverter notation, which is something really annoying.

I always hated to do so, which forced me to write an extra method that encapsulates this inverter, because most of the times either I don’t notice it, or it takes me more time to understand the scenario then invert it.

here is a simple scenario checking if the user is currently activated.

<?phpnamespace App\Models;use Illuminate\Foundation\Auth\User as Authenticatable;class User extends Authenticatable
{
public function isActive()
{
return $this->active;
}
}

and somewhere in my project I should do that

if($user->isActive()) {  // code.
}

for inverting this, I will always have to do this.

if(!$user->isActive()) {   // code.
}

I don’t know what about you, but this not notation drives me mad when I see it, so I tend to encapsulate it in my model like so

class User extends Authenticatable
{
public function isActive()
{
return $this->active;
}
public function isNotActive()
{
return !$this->active;
}
}

I’ve been doing this for long time to escape using the not notation within my conditions, but recently I created my own helper for that which is “unless” helper, this really helped me to give up on writing an inverting method for every single method that returns a boolean.

You probably used the optional helper that Laravel provides, which checks on the passed value and if exists, it passes it to a callback and execute your own callback.

luckily the optional class that laravel uses for this helper has a macro trait which means that we can push our own methods to this class, to use it later.

at your AppServiceProvider file, you can do the following.

use Illuminate\Support\Optional;use Illuminate\Support\ServiceProvider;class AppServiceProvider extends ServiceProvider
{
public function register()
{
Optional::macro('unless', function (\Closure $callback) {
if ($this->value) {
return $callback($this->value);
}
return false;
});
}
}

In order to use it, we will have to call the optional class, so rather than doing this, we can create our own helper for this.

if you don’t know how to register your own helper, you can read this

if (!function_exists('unless')) {
function unless($value = null, callable $callback)
{
if (!$value) {
return (new Optional(!$value))->unless($callback);
}
}
}

throughout this helper, now you can pass your value/condition which will be inverted, and pass the second parameter which is your callback, the returned value of your callback will be returned back to you, which i find it way cleaner and flexible.

now if we die and dump this to check on what’s happening.

unless(false, function ($value) {
dd($value);
});
// returns true.

if we return a custom thing from the callback, the unless function will return it.

dd(unless(false, function ($value) {
return 'here';
}));
// returns 'here'

if we pass a condition/value that will be inverted and casted to false, the unless function will return null as the callback won’t be executed.

dd(unless(true, function ($value) { return 'here';
});
// returns null

--

--