PHPackages                             mr-feek/eloquent-null-casts - 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. [Database &amp; ORM](/categories/database)
4. /
5. mr-feek/eloquent-null-casts

ActiveLibrary[Database &amp; ORM](/categories/database)

mr-feek/eloquent-null-casts
===========================

Model attributes that are null will be cast if they are listed in the casts array. Unlike default Eloquent.

v0.1(6y ago)03MITPHPPHP ^7.1

Since Apr 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mr-feek/eloquent-null-casts)[ Packagist](https://packagist.org/packages/mr-feek/eloquent-null-casts)[ Docs](https://github.com/mr-feek/eloquent-null-casts)[ RSS](/packages/mr-feek-eloquent-null-casts/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Allow eloquent to cast null values of attributes
================================================

[](#allow-eloquent-to-cast-null-values-of-attributes)

[![Packagist](https://camo.githubusercontent.com/9a203d185af943d66110170da181c85f846ed7af0cc1574e3f2e300689ff8eba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d722d6665656b2f656c6f7175656e742d6e756c6c2d63617374732e737667)](https://packagist.org/packages/mr-feek/eloquent-null-casts)[![Packagist](https://camo.githubusercontent.com/4b5bc71bfaf48fb2e8186f7d6c868c36b000a3afa6ff5270cc76696bb4647618/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d722d6665656b2f656c6f7175656e742d6e756c6c2d63617374732e737667)](https://packagist.org/packages/mr-feek/eloquent-null-casts)[![dev-master Tests](https://camo.githubusercontent.com/db9cc4e26b326a6f5246f8ea768ec04363cd65e292536d354671d97802c697f2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d722d6665656b2f656c6f7175656e742d6e756c6c2d63617374732f52756e25323054657374733f6c6162656c3d6465762d6d61737465722532307465737473)](https://camo.githubusercontent.com/db9cc4e26b326a6f5246f8ea768ec04363cd65e292536d354671d97802c697f2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d722d6665656b2f656c6f7175656e742d6e756c6c2d63617374732f52756e25323054657374733f6c6162656c3d6465762d6d61737465722532307465737473)

By default, Laravel does not allow casting null values of attributes listed in the `casts` array. This isn't always desirable, as sometimes you want to ensure an attribute is always a boolean. This package provides a simple trait to override this behavior on a per Model basis. It isn't always feasible to run a migration to no longer allow null in the database.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mr-feek/eloquent-null-casts
```

Usage
-----

[](#usage)

In PHP we cannot override a trait's property in the class where the trait is used, so we have two implementation choices.

First option is to use the `CastsNullAttributes` trait and define the casts in the constructor.

```
