承接 stidges/laravel-db-normalizer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stidges/laravel-db-normalizer

Composer 安装命令:

composer require stidges/laravel-db-normalizer

包简介

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

README 文档

README

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.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 70
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 70
  • Watchers: 6
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固