laravel 如何从类中获取数组数据

k5ifujac  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(128)

我如何从这个类数组中检索带有国家代码的翻译,并在我的blade文件中显示它?假设我有国家代码AF,那么我如何返回阿富汗?

class TranslatedCountryCodes
{

    private        $translatedCodes = [];
    private static $instance;

    protected function __construct()
    {
        $this->translatedCodes = [
            'AF' => __('Afghanistan', 'countrycode', 'myproject'),
            'AX' => __('Aland Islands', 'countrycode', 'myproject')
        ];
    }

    public function commitTranslation()
    {
        return $this->translatedCodes;
    }

    /**
     * @return static
     */
    public static function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static();
        }

        return static::$instance;
    }
}

我试过了

TranslatedCountryCodes::commitTranslation('AF')

我一直在查找一些辅助函数,但无法找到

2lpgd968

2lpgd9681#

您可以:

TranslatedCountryCodes::getInstance()->commitTranslation()['AF']

相关问题