Normalize all database results to one unified interface, to make swapping repositories a breeze.

0.1.0 2014-03-12 20:38 UTC


Latest Stable Version Total Downloads Build Status License

This Laravel package allows you to easily swap out your repository implementations, by providing a unified interface to your database results.

It intercepts your results and turns them into collections and entities. That way, whether you are using Eloquent, the Query Builder or any other implementation, the results will be the same!


Getting Started

This package can be installed through Composer, just add it to your composer.json file:

    "require": {
        "stidges/laravel-db-normalizer": "0.*"

After you have added it to your composer.json file, make sure you update your dependencies:

composer install

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:


When using Eloquent models, they should extend the NormalizableModel class:

use Stidges\LaravelDbNormalizer\NormalizableModel;

class User extends NormalizableModel
    // ...
2. Disable auto-normalization:

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!

use Stidges\LaravelDbNormalizer\Normalizer;


// Example using the query builder
$result = DB::table('users')->get();
// ... Do stuff with the result.
$normalized = with(new Normalizer)->normalize($result);

// Example using Eloquent
$user = User::find(1);
// ... Do stuff with the user.
$normalized = with(new Normalizer)->normalize($user->toArray());

// Example using Eloquent collection
$users = User::all();
// ... Do stuff with the users.
$normalized = with(new Normalizer)->normalize($users->toArray());

Using the normalized results

This package provides 2 classes:

  1. A Collection class. As it currently stand, this class just extends Laravel's Illuminate\Support\Collection class.
  2. 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.

Example usage

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.