All Projects → stidges → Laravel Db Normalizer

stidges / Laravel Db Normalizer

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

Projects that are alternatives of or similar to Laravel Db Normalizer

Larawiz
Larawiz is a easy project scaffolder for Laravel
Stars: ✭ 28 (-61.11%)
Mutual labels:  database, laravel
Laravel Pg Extensions
Laravel extensions for Postgres
Stars: ✭ 33 (-54.17%)
Mutual labels:  database, laravel
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+1215.28%)
Mutual labels:  database, laravel
Learning laravel kernel
Laravel核心代码学习
Stars: ✭ 789 (+995.83%)
Mutual labels:  database, laravel
Laravel Translatable
It's a Laravel database translations manager
Stars: ✭ 47 (-34.72%)
Mutual labels:  database, laravel
Eloquent Driver
A package that allows you to store Statamic entries in a database.
Stars: ✭ 28 (-61.11%)
Mutual labels:  database, laravel
Laravel Couchbase
Couchbase providers for Laravel
Stars: ✭ 31 (-56.94%)
Mutual labels:  database, laravel
Analogue
Analogue ORM : Data Mapper ORM for Laravel/PHP
Stars: ✭ 618 (+758.33%)
Mutual labels:  database, laravel
Laravel Tags
Add tags and taggable behaviour to your Laravel app
Stars: ✭ 1,026 (+1325%)
Mutual labels:  database, laravel
Avbook
AV 电影管理系统, avmoo , javbus , javlibrary 爬虫,线上 AV 影片图书馆,AV 磁力链接数据库,Japanese Adult Video Library,Adult Video Magnet Links - Japanese Adult Video Database
Stars: ✭ 8,133 (+11195.83%)
Mutual labels:  database, laravel
Laravel
Laravel Model Generator
Stars: ✭ 715 (+893.06%)
Mutual labels:  database, laravel
Prequel
Prequel for Laravel. Clear and concise database management.
Stars: ✭ 1,141 (+1484.72%)
Mutual labels:  database, laravel
Laravel Options
Global key-value store in the database
Stars: ✭ 626 (+769.44%)
Mutual labels:  database, laravel
Laravel Optimistic Locking
Adds optimistic locking feature to eloquent models.
Stars: ✭ 71 (-1.39%)
Mutual labels:  database, laravel
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+766.67%)
Mutual labels:  database, laravel
Laravel Database Logger
Log database query sql in Laravel Application, support Guard,Auth to multiple file record
Stars: ✭ 31 (-56.94%)
Mutual labels:  database, laravel
Laravel Backup
A package to backup your Laravel app
Stars: ✭ 4,752 (+6500%)
Mutual labels:  database, laravel
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+634.72%)
Mutual labels:  database, laravel
Laravel Queue Database Ph4
Laravel Database Queue with Optimistic locking
Stars: ✭ 37 (-48.61%)
Mutual labels:  database, laravel
Laravel Cadillac
🍺 A database tool for laravel.
Stars: ✭ 63 (-12.5%)
Mutual labels:  database, laravel

Laravel DB Normalizer

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!

Result

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:

'Stidges\LaravelDbNormalizer\DbNormalizerServiceProvider',

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!

Contributing

All suggestions and pull requests are welcome! If you make any substantial changes, please provide tests along with your pull requests!

License

Copyright (c) 2014 Stidges - Released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].