Laravel 5.0 - Eloquent Attribute Casting
This is a series of posts on New Features in Laravel 5.0.
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
I had completely forgotten to finish my Laravel 5.0 blog posts, but I saw a great quick introduction to attribute casting at Laravel 5 Eloquent Attribute Casting is Awesome, so I figured I would add it to my feature list. Check out the official Eloquent docs here.
What is attribute casting?
Casting a value means changing it to (or ensuring it is already) a particular type. Some types you might be familiar with are integer
or boolean
.
Attribute casting is a feature of Eloquent models that allows you to set your model to automatically cast a particular attribute on your Eloquent model to a certain type.
Note: You could do this in the past, but you would have to automatically define a mutator for each attribute; now you can do it automatically with a single configuration array.
That means if you store your data in a particular format in the database, and you want it to return in a different format, you can now cast it to the new format.
But why?
The most common uses for this will be when you store numbers—they’re returned as strings by default, but Eloquent attribute casting allows you to cast them as integer
, real
, float
, or double
—or booleans—you can convert 0
and 1
in your database to true
and false
.
But that’s not all.
How does it work?
You cast attributes in Eloquent by adding a protected $casts
array to your model.
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'is_admin' => 'boolean',
];
As you can see, each entry in the array has the property slug as the key, and the cast type as the value. This $casts
array is telling Eloquent: “Every time I access a property on this model named is_admin
, please return it cast to type boolean
.
The cast types
integer
(or int
)
This casts your field to an integer using return (int) $value
.
float
(or real
or double
)
Real, Float, and Double are the same thing in PHP. PHP’s (double)
and (real)
type casting are just aliases to (float)
; and if you check out the source, Eloquent is literally running return (float) $value
for all three of these keys.
string
This casts your field to a string using return (string) $value
.
boolean
(or bool
)
This casts your field to a boolean using return (bool) $value
, which means you’ll likely be storing your values as 0
and 1
.
object
Object and Array are the most interesting option. Both convert (deserialize) JSON-serialized arrays into PHP. Object uses return json_decode($value)
, returning a stdClass object.
array
Array deserializes JSON-serialized arrays into PHP arrays, using return json_decode($value, true)
, returning an array.
You can view the actual code for these in the source.
Conclude
As you can see, Eloquent attribute casting has a ton of potential to free us up from unnecessary repetitive logic, and also sneakily makes it a lot easier to store data in JSON in our database. Good Stuff!
Comments? I'm @stauffermatt on Twitter
Tags: laravel 5 • laravel • eloquent
This is part of a series of posts on New Features in Laravel 5.0:
-
Sep 10, 2014 | laravel, 5.0, laravel 5
-
Sep 10, 2014 | laravel, 5.0, laravel 5
-
Sep 12, 2014 | laravel, laravel 5, 5.0
-
Sep 20, 2014 | laravel, 5.0, laravel 5
-
Sep 28, 2014 | laravel, laravel 5, 5.0
-
Sep 30, 2014 | laravel, 5.0, laravel 5
-
Oct 9, 2014 | laravel, 5.0, laravel 5
-
Oct 10, 2014 | laravel, 5.0, laravel 5
-
Oct 10, 2014 | laravel, 5.0, laravel 5
-
Oct 16, 2014 | laravel, 5.0, laravel 5
-
Nov 20, 2014 | laravel, 5.0, laravel 5
-
Jan 2, 2015 | laravel, 5.0, commands, laravel 5
-
Jan 16, 2015 | laravel, laravel 5
-
Jan 19, 2015 | laravel 5, laravel
-
Jan 21, 2015 | laravel, events, 5.0, laravel 5
-
Jan 26, 2015 | laravel, laravel 5
-
Feb 1, 2015 | laravel, laravel 5
-
Feb 14, 2015 | laravel 5, laravel, eloquent