问题
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