2017 © Pedro Peláez
 

library laravel-revaluation

Laravel 5 model revaluation helper.

image

overtrue/laravel-revaluation

Laravel 5 model revaluation helper.

  • Friday, May 18, 2018
  • by overtrue
  • Repository
  • 2 Watchers
  • 27 Stars
  • 1,372 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 12 Versions
  • 6 % Grown

The README.md

Laravel Revaluation

Laravel 5 model revaluation helper., (*1)

Build Status Latest Stable Version Latest Unstable Version Scrutinizer Code Quality Code Coverage Total Downloads License , (*2)

Installation

You can install the package using composer, (*3)

$ composer require overtrue/laravel-revaluation -vvv

Then add the service provider to config/app.php, (*4)

Overtrue\LaravelRevaluation\RevaluationServiceProvider::class,

Publish the config file:, (*5)

$ php artisan vendor:publish --provider='Overtrue\LaravelRevaluation\RevaluationServiceProvider'

Finally, use Overtrue\LaravelRevaluation\Traits\HasRevaluableAttributes in model. And specify which attributes in the $revaluable property can be revalued:, (*6)

<?php

use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelRevaluation\Traits\HasRevaluableAttributes;

class Order extends Model
{
    use HasRevaluableAttributes;

    // 1. Use the default valuator.
    protected $revaluable = [
        'total', 'paid_in', 'postage',
    ];

    // 2. Use the specified valuator:
    // protected $revaluable = [
    //    'foo' => '\Foo\Support\Valuator\Foo',
    //    'bar' => '\Foo\Support\Valuator\Bar',
    //    'baz',  // default valuator
    //];

    //...
}

Usage

Basic usage with default options.

$order = Order::find(1);

$order->total;                      // 345 (Db: 34500)
$order->raw_total;                   // 34500

$order->getRevaluatedTotalAttribute() or $order->revaluated_total; // Overtrue\LaravelRevaluation\Valuators\RmbCent
$order->revaluated_total->inYuan();       // 345.00
$order->revaluated_total->asCurrency();   // ¥345.00

// automatic setter.
$order->total = 123;
$order->save();

$order->total;                      // 123
$order->raw_total;                  // 12300
$order->revaluated_total->asCurrency();   // ¥123.00

// to array
$order->toArray();
//[
//    'total' => 12300,
//    'revaluated_total' => 123.0,
//]

Custom revaluated attribute prefix

protected $revaluatedAttributePrefix = 'display';

$order->total;                      // 123.0;
$order->raw_total;                  // 12300
$order->display_total->asCurrency();   // ¥123.00

// to array
$order->toArray();
//[
//    'total' => 12300,
//    'display_total' => 123.0,
//]

Disable auto append revaluated attributes to array

protected $appendRevaluatedAttributesToArray = false;

$order->total;                      // 123.0;
$order->raw_total;                  // 12300
$order->display_total->asCurrency();   // ¥123.00

// to array
$order->toArray();
//[
//    'total' => 12300,
//]

Using revaluated value replace raw attributes value

protected $replaceRawAttributesToArray = true;

$order->total;                      // 123.0;
$order->raw_total;                  // 12300
$order->display_total->asCurrency();   // ¥123.00

// to array
$order->toArray();
//[
//    'total' => 123.0,
//]

More usage examples, Please refer to unit testing, (*7)

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?, (*8)

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》, (*9)

License

MIT, (*10)

The Versions

18/05 2018

dev-master

9999999-dev

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

17/05 2018

dev-analysis-zDdv0b

dev-analysis-zDdv0b

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

28/03 2018

1.0.5

1.0.5.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

29/01 2018

1.0.4

1.0.4.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

29/01 2018

dev-analysis-zDmpPB

dev-analysis-zDmpPB

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

19/01 2018

1.0.3

1.0.3.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

17/11 2017

1.0.2

1.0.2.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

30/08 2017

1.0.1

1.0.1.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

05/07 2017

1.0.0

1.0.0.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

03/07 2017

0.1.0

0.1.0.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

15/03 2017

0.0.2

0.0.2.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue

08/03 2017

0.0.1

0.0.1.0

Laravel 5 model revaluation helper.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar overtrue