Last active
February 2, 2020 12:16
-
-
Save muhep06/1a73462fcdf2d6ffd4bc566af9f8ffb8 to your computer and use it in GitHub Desktop.
Laravel Nova Custom Currency Field - call undefined function money_format() error fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Fields; | |
use Laravel\Nova\Fields\Number; | |
use NumberFormatter; | |
class Money extends Number | |
{ | |
public $locale = "en_GB"; | |
public $currency = "EUR"; | |
/** | |
* Create a new field. | |
* | |
* @param string $name | |
* @param string|null $attribute | |
* @param mixed|null $resolveCallback | |
* @return void | |
*/ | |
public function __construct($name, $attribute = null, $resolveCallback = null) | |
{ | |
parent::__construct($name, $attribute, $resolveCallback); | |
$this->step('0.01')->displayUsing(function ($value) { | |
$formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); | |
return !is_null($value) ? $formatter->formatCurrency($value, $this->currency) : null; | |
}); | |
} | |
/** | |
* @param string $locale | |
* @return $this | |
*/ | |
public function locale(string $locale) | |
{ | |
$this->locale = $locale; | |
return $this; | |
} | |
/** | |
* @param string $currency | |
* @return $this | |
*/ | |
public function currency(string $currency) | |
{ | |
$this->currency = $currency; | |
return $this; | |
} | |
} |
Author
muhep06
commented
Feb 1, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment