Laravel模型属性在视图中

时间:2017-05-31 06:34:59

标签: laravel laravel-5

我想在视图中访问我的模型类的静态属性

我试过了

{{ \App\task::$accounts_currency }}
{{ \App\task::accounts_currency }}

但似乎没什么用。请指导 感谢

我的模特课

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class task extends Model
{
    protected $fillable = [
        'title',
        'description'
    ];

    public static $company_name = 'SolutionsLtd.';
    public static $company_address_l1 = "Adress";
    public static $company_address_l2 = "Cell# 11111111111";
    public static $company_address_l3 = "Pabx# 1111111111111";
    public static $reports_prefix = "LS_";
    public static $invoices_prefix = "LS-";
    public static $accounts_currency = "USD";
    public static $currency_symbol = "USD.";
    public static $financial_year = "07-01"; // MM-DD
}

错误:

  

87347301c85619fc6de3f6b6738745f2第105行中的FatalErrorException:   访问未声明的静态属性:App \ task :: $ accounts_currency

2 个答案:

答案 0 :(得分:0)

您可以尝试一下:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class task extends Model
{
    protected $fillable = [
        'title',
        'description'
    ];

    public static $company_name = 'SolutionsLtd.';
    public static $company_address_l1 = "Adress";
    public static $company_address_l2 = "Cell# 11111111111";
    public static $company_address_l3 = "Pabx# 1111111111111";
    public static $reports_prefix = "LS_";
    public static $invoices_prefix = "LS-";
    public static $accounts_currency = "USD";
    public static $currency_symbol = "USD.";
    public static $financial_year = "07-01"; // MM-DD


}

查看

{{ $accounts_currency }}

<强>更新

<强>控制器

use App\task;

public function datas(){
         $accounts_currency = task::accounts_currency;
         return view('yourView', compact('accounts_currency'));
       }

如果您有任何疑虑,请在下方发表评论。

答案 1 :(得分:0)

你可以这样做:

在模型中

添加属性:

public $accounts_currency = "USD";

和功能:

public static function getAccountsCurrency(){
    return (new self)->accounts_currency;
}

并在您的视图中添加:

{{ \App\task::getAccountsCurrency() }}

实现1:

你也可以这样做:

public static function getValues(){
    $instance = new self;
    $values =   ["accounts_currency" => $instance->accounts_currency,
                "company_name" => $instance->company_name];
    return $values;
}

并在您看来:

{{ \App\task::getValues()["company_name"] }}