Using number_format method in Laravel

后端 未结 4 2281
傲寒
傲寒 2021-02-06 22:52

I am fairly new in Laravel and Blade templating.
Can anyone help show me how to do this?

I have a view like this:

@foreach ($Expen         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 23:27

    Here's another way of doing it, add in app\Providers\AppServiceProvider.php

    use Illuminate\Support\Str;
    
    ...
    
    public function boot()
    {
        // add Str::currency macro
        Str::macro('currency', function ($price)
        {
            return number_format($price, 2, '.', '\'');
        });
    }
    

    Then use Str::currency() in the blade templates or directly in the Expense model.

    @foreach ($Expenses as $Expense)
        
            {{{ $Expense->type }}}
            {{{ $Expense->narration }}}
            {{{ Str::currency($Expense->price) }}}
            {{{ $Expense->quantity }}}
            {{{ Str::currency($Expense->amount) }}}                                                            
        
    @endforeach
    

提交回复
热议问题