Package Data | |
---|---|
Maintainer Username: | cviebrock |
Maintainer Contact: | colin@viebrock.ca (Colin Viebrock) |
Package Create Date: | 2014-07-12 |
Package Last Update: | 2015-07-13 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-19 03:04:17 |
Package Statistics | |
---|---|
Total Downloads: | 58,604 |
Monthly Downloads: | 649 |
Daily Downloads: | 23 |
Total Stars: | 24 |
Total Watchers: | 6 |
Total Forks: | 1 |
Total Open Issues: | 0 |
A trait that allows a Laravel project's Eloquent models to cast attribute values to native PHP variable types.
For some database drivers, all the attributes you get back from a query are returned as strings, even when the underlaying column-type is INTEGER or FLOAT or BOOLEAN.
Rather than have to use these "integer-looking" strings, etc., and rely on PHP's type-juggling, this trait will cast those attribute values to the proper native PHP variable type automagically for you.
This is also going to be very handy if you are, say, building an API and would like to just return JSON-versions of Eloquent models. Using this trait, all the JSON elements are going to be the right type for consumers of your API -- instead of all strings -- saving them type-juggling on their end.
Note: I believe if you are using the mysqlnd drivers in your PHP installation, then you don't need this trait as mysqlnd handles this type casting for you. Try it out by doing a
var_dump($model->getKey())
. If it shows that the value is an integer, you don't need this package. If it shows it's a string, read on.
In your project's composer.json file:
"require": {
"cviebrock/eloquent-typecast": "1.*"
}
In your project's models (or your own base model):
use Cviebrock\EloquentTypecast\EloquentTypecastTrait;
class MyModel {
use EloquentTypecastTrait;
// Define the attributes you want typecast here
protected $cast = array(
'id' => 'integer',
'price' => 'float',
'is_awesome' => 'boolean'
);
...
}
That's it. No service providers or facades required. Because it's a trait, however, you will need to be running PHP 5.4 or later.
Anytime you request an attribute listed in the $cast
array, it will be converted from the (usually) string that your database returned into a the native PHP variable type you specified.
The keys of the $cast
array are the attribute (i.e. column) names, and the values are the types you want to cast to. Anything supported by PHP's settype() function is valid ... although casting to arrays, objects, or null could be problematic.
If you set the $castOnSet
property on your model to true
, then setting an attribute that's in the $cast
array will typecast that value before setting it. For example:
class MyModel {
use EloquentTypecastTrait;
protected $castOnSet = true;
protected $cast = array(
'price' => 'float',
);
}
$myModel = MyModel::find(1);
$price = Input::get('price'); // this will be a string
$myModel->price = $price; // the string is cast to a float before setting;
In general, this setting isn't really necessary as Laravel and most databases will handle the string-to-column-type conversion for you on save. However, maybe there are cases where it's useful, so it's added for "feature completion".
Because of the way the trait works, you should make sure that your $cast
array does not include:
$model->toArray()
triggers the casting as well. $model->getAttributes()
, however, does not. It returns the raw values from the query (not even the date mutation).
Please use Github for bugs, comments, suggestions.
composer install --dev
in the root directory to install required testing packages.eloquent-typecast/tests/TypecastTest.php
.vendor/bin/phpunit
to the new (and all previous) tests and make sure everything passes.develop
branch.Please note that you must create your pull request against the develop
branch.
Eloquent-Typecast was written by Colin Viebrock and released under the MIT License. See the LICENSE.md file for details.
Copyright 2014 Colin Viebrock