PHPackages                             imanghafoori/laravel-nullable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. imanghafoori/laravel-nullable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imanghafoori/laravel-nullable
=============================

A package to help you write expressive defensive code in a functional manner

v1.2.11(1y ago)151423.7k↓17.5%10[1 PRs](https://github.com/imanghafoori1/laravel-nullable/pulls)6MITPHPPHP ^7.1.3|7.2.\*|7.3.\*|7.4.\*|8.\*CI failing

Since Jul 21Pushed 7mo ago7 watchersCompare

[ Source](https://github.com/imanghafoori1/laravel-nullable)[ Packagist](https://packagist.org/packages/imanghafoori/laravel-nullable)[ Docs](https://github.com/imanghafoori1/laravel-nullable)[ RSS](/packages/imanghafoori-laravel-nullable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (18)Used By (6)

 Laravel Nullable
=================

[](#-laravel-nullable)

Do not let "null" to impersonate your objects.
----------------------------------------------

[](#do-not-let-null-to-impersonate-your-objects)

[![](https://user-images.githubusercontent.com/6961695/63855847-9524ef00-c9b5-11e9-92dc-9e5232741199.png)](https://user-images.githubusercontent.com/6961695/63855847-9524ef00-c9b5-11e9-92dc-9e5232741199.png)

[![StyleCI](https://camo.githubusercontent.com/eb20fd626fa8b25bb725cb77f91779c724bc48ad/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3139383034383931382f736869656c643f6272616e63683d616e616c797369732d586b33453479)](https://github.styleci.io/repos/198048918)[![Quality Score](https://camo.githubusercontent.com/41bca3726697592f356f1e789d37997aac201519/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f696d616e676861666f6f7269312f6c61726176656c2d6e756c6c61626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/imanghafoori1/laravel-nullable)[![Code Coverage](https://camo.githubusercontent.com/1eeff0bf14f35448520631b23321844d90dbe986/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d616e676861666f6f7269312f6c61726176656c2d6e756c6c61626c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/imanghafoori1/laravel-nullable/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/ad783a174a9a32fc6415825c41eb8d631f43c82e/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f6c61726176656c2d6e756c6c61626c652f762f737461626c65)](https://packagist.org/packages/imanghafoori/laravel-nullable)[![PHP from Packagist](https://camo.githubusercontent.com/53867af7105346d348a9fce0d888c67ff4498262/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6469706c6f646f636b65722f636f6d6d656e74732d6c6f616465722e7376673f636f6c6f723d386139326262266c6f676f3d706870266c6f676f436f6c6f723d666666)](https://camo.githubusercontent.com/53867af7105346d348a9fce0d888c67ff4498262/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6469706c6f646f636b65722f636f6d6d656e74732d6c6f616465722e7376673f636f6c6f723d386139326262266c6f676f3d706870266c6f676f436f6c6f723d666666)[![License](https://camo.githubusercontent.com/c80bc97504e609e27ff81f3fa18c7c500104a7aa/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f6c61726176656c2d616e79706173732f6c6963656e7365)](https://packagist.org/packages/imanghafoori/laravel-anypass)

### Functional programming paradigm in laravel

[](#functional-programming-paradigm-in-laravel)

#### Built with ❤️ for every smart laravel developer

[](#built-with-heart-for-every-smart-laravel-developer)

`Null` is usually used to represent a missing value (for ex when we can't find a row with a partcular Id we return null) And that is the BAD IDEA, we are going to kill off !!!

### 🔥 Installation:

[](#fire-installation)

```
composer require imanghafoori/laravel-nullable

```

This package exposes a `nullable()` global helper function with which you can wrap variables which sometimes are object and sometimes `null`.

Consider this:

```
$email = TwitterApi::find(1)->email;
```

Now this code is working fine But...

What if the user with ID of 1 gets deleted in future ?!

`null->email ` and crap ! 😧

So if you forget to handle the null with an if statement, you will have errors.

You need something to FORCE you and the users of your class methods to handle the `null` cases.

To prevent such errors, you should code like this:

```
$user = $twitterApi->find($id);

if ($user === null) {
    return redirect()->route('page_not_found');
}
```

#### ▶️ Nullables to rescue !!!

[](#arrow_forward-nullables-to-rescue-)

To refactor the code above, first

You have to change your repo class :

```
// the old way:

/**
* @return User|null
