After you have added it to your composer.json file, make sure you update your dependencies:
Next, you can do either of these two:
1. Enable auto-normalization:
By registering this package's ServiceProvider class, all the queries you run will be automatically normalized to the unified Collection and Entity classes. Add the following line to your app/config/app.php file:
If you would rather want some more control, don't register the ServiceProvider. That way you can control when the results get cast to the classes. To do this, use the Normalizer class. Make sure you always pass an array to the normalizer!
//...// Example using the query builder$result = DB::table('users')->get();
// ... Do stuff with the result.$normalized = with(newNormalizer)->normalize($result);
// Example using Eloquent$user = User::find(1);
// ... Do stuff with the user.$normalized = with(newNormalizer)->normalize($user->toArray());
// Example using Eloquent collection$users = User::all();
// ... Do stuff with the users.$normalized = with(newNormalizer)->normalize($users->toArray());
Using the normalized results
This package provides 2 classes:
A Collection class. As it currently stand, this class just extends Laravel's Illuminate\Support\Collection class.
An Entity class. This class can contain nested entities and collections (relations). It provides a fluent interface to accessing the attributes of the entity, and can be cast to an array or JSON using the familiar toJson and toArray methods. On top of that, it provides a getDirtyAttributes() function, which allows you to get all the attributes that were changed after creation.
For examples on how to use the package, please view the examples directory!
All suggestions and pull requests are welcome! If you make any substantial changes, please provide tests along with your pull requests!
Copyright (c) 2014 Stidges - Released under the MIT license.