Jens Segers on Jan 03 2015

Laravel 5 Eloquent attribute casting is awesome

Attribute casting is a new feature of Laravel's Eloquent model that was recently added without really getting much attention (yet). So what does it do, and how can you use it?

Casting is, implicitly or explicitly, changing the entity of one data type into another. A simple example is casting the string "100" to its integer representation:

$value = "100"; // $value is a string
$value = (int) "100"; // $value is an integer

In Laravel 4, you can add manual attribute casting by adding accessors and/or mutators to manipulate the attribute value:

public function getAgeAttribute($value)
    return (int) $value;

But not anymore! Laravel 5 recently added attribute casting which makes this process a bit easier. To cast specific attributes, add them to the $casts property of the model class with their desired type:

protected $casts = [
    'age' => 'integer'

Now, whenever you access the age attribute, it will be converted to an integer by Eloquent.

var_dump($user->age); // int(25)

For me personally, this will be especially useful when working with boolean values. Boolean values are usually stored as a TINYINT in the database, and when Eloquent fetches the model again, they are returned as a "1" or "0" string. "Luckily" for us, PHP does some type juggling and implicitly converts it to the correct boolean values. But shit will hit the fan if you pass the JSON version of your model to javascript.

user = {
    "id": "1",
    "username": "jens",
    "activated": "0"

user.activated ? "yes" : "no"; // will output "yes"

By adding the activated property to new $casts property, the value will be automatically converted to a boolean, and won't cause problems in javascript.

    "id": "1",
    "username": "jens",
    "activated": true


B. Wes. 6 months ago

Thank you but, What if we want to get all user.activated = 1 with eloquent now ?

aamir 1 year ago

when i insert id manually from controller it takes it as string. I want to insert it as object id. Here is my code $dialog=dialog::create(array( '_id'=>(object)$request->dialog[$key]['dialogid']', '_class'=>'', 'message'=>$request->dialog[$key]['message'], 'options'=>$request->dialog[$key]['buttons'] ));

aamir 1 year ago

when i insert id manually from controller it takes it as string. I want to insert it as object id. Here is my code $dialog=dialog::create(array( '_id'=>(object)$request->dialog[$key]['dialogid']', '_class'=>'', 'message'=>$request->dialog[$key]['message'], 'options'=>$request->dialog[$key]['buttons'] ));

Sheyla 1 year ago

Thank you! Really helpful information!

DapoBelieve 1 year ago

nice post

Mahdi Pishguy 1 year ago


chaku Shirsat 1 year ago


Droid Mechanic 1 year ago


terrasoff 2 years ago

... awesome until you gonna make your own casting type

Cortex Code 3 years ago

Thanks, this saved me a lot of time. :-)

Paulo Coghi 3 years ago

Sorry for the (semi)off-topic comment, but do you plan to create a "laravel-cassandra" project, following the same brilliant approach of laravel-mongodb?

asdasd 4 years ago


lloople 4 years ago

It's awesome, but I'm having trouble to save the data back to database. I cast a json string like 'object' and works pretty well, bu I tried to add new properties to that object $item->object_field->testing = true and then $item->save(); but the json string is not modified in the database. Any idea?

farzam 4 years ago

awesome post tnx for share with us

faruh 4 years ago

Very useful

mmachnicki 4 years ago

good to know, tnx ;)

dhrubojyoti das 4 years ago

Simple yet very very useful I just love Laravel

morrelinko 4 years ago

Nice Info

Miguel 4 years ago

Well written! :)

demo 4 years ago