laravel access to model constant in blade

喜你入骨 提交于 2020-04-13 07:22:08

问题


Need access model constant in blade not with full path

class PaymentMethod extends Model {

const PAYPAL_ACCOUNT      = 'paypal_account';
const CREDIT_CARD         = 'credit_card';

and in blade

        {{ App\Classes\Models\PaymentMethod::CREDIT_CARD }}

work

but

        {{ PaymentMethod::CREDIT_CARD }}

throws Class 'PaymentMethod' not found


回答1:


You may use aliases:

in your config\app.php under aliases section :

aliases => [
     ....
    'PaymentMethod' => App\Classes\Models\PaymentMethod::class
]

then use it in your balde file

{{ PaymentMethod::CREDIT_CARD }}



回答2:


Found this thread while trying to solve the same problem. Decided to go with the following approach:

namespace App\Services\Auth\IAM;

class IAMConstants
{
    const GUARD_WEB = 'web';
    const GUARD_ADMIN = 'admin';
}

Then in Blade:

@inject('constants', 'App\Services\Auth\IAM\IAMConstants')
...
<option value="{{ $constants::GUARD_WEB }}">App user</option>

The injected class should be small since it will carry in all its dependencies: https://laravel.com/docs/5.4/blade#service-injection




回答3:


Just try this

{{ trans('lang.LANG_CONST_NAME') }} 

Assuming that the lang.php file exists under the lang folder in the resources. For example, in my case \lang\en\lang.php



来源:https://stackoverflow.com/questions/47270905/laravel-access-to-model-constant-in-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!