laravel/framework — pull request #5004 by JosephSilber (2014-07-10 00:11:15+00:00)
Title: Add casting to Eloquent

This PR is based on the work done by @daylerees in #4948. See that PR for more info.
```php
class Book extends Eloquent
{
protected $casts = [
'name' => 'string',
'pages' => 'integer',
'read' => 'boolean',
'read_on' => 'date',
'price' => 'double',
];
}
```

It basically allows any type that [`settype`](http://www.php.net/manual/en/function.settype.php) accepts, plus `date` (not sure how useful `array`, `object` and `null` could be. Maybe we should prevent that?).

---

This improves upon @daylerees's work thus:

1. The casts are cached so they don't have to be recalculated for every attribute.
2. The casts are performed when the attributes are set, so that there's no need to mutate again on get.
3. Allows for traits to add their own casts. This has been implemented here in the `SoftDeletingTrait` to cast `deleted_at` to a date (it had been working automatically in 4.1 and has been restored here).

---

There are no tests yet. I'm trying to get a consensus about what people think of this implementation and if @taylorotwell thinks this should be added (and whether to 4.2 or 4.3).

Once everything is ironed out I'll be more than happy to write the tests.