2017-05-31 93 views
0

我要访问的观点Laravel模型属性鉴于

我的模型类的静态属性我已经试过

{{ \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 
} 

错误:

FatalErrorException在87347301c85619fc6de3f6b6738745f2行105: 访问未申报的静态属性:应用程序\任务:: $ accounts_currency

+0

为什么不能让一个方法,其中,将返回所有?比如'public function getStatics(){return ['company_name'=> $ this-> company_name,'e​​tc'=> $ this-> etc]; }' – Demonyowh

+0

你有什么错误吗? –

+0

@AndreyLutskevich它说错误:FatalErrorException 87347301c85619fc6de3f6b6738745f2第105行:访问未声明的静态属性:App \ task :: $ accounts_currency – Peter

回答

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 }}

UPDATE

控制器

use App\task; 

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

如果您有任何疑问,请在下面留言。

+0

我不愿意传递给任何视图只是想在其他视图中访问这个类变量.... 函数返回self :: accounts_currency; ..在视图 echo \ App \ task :: datas();但似乎不起作用 – Peter

+0

即使更新视图名称,它仍然无法正常工作 public function datas(){ $ accounts_currency = self :: accounts_currency; return view('voucher.profit-loss.blade',compact('accounts_currency')); } 输出未定义变量:accounts_currency(查看: – Peter

+0

hm,如果您只想访问它,一个选项是使用'require_once',然后使用您的代码来获取静态属性 –

0

你可以做somethig这样的:

在模型

添加属性:

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"] }} 
+0

如果你有很多项目要返回到视图,你不能这么做。它会变得沉重。 –

+0

它说调用未定义的方法Illuminate \ Database \ Query \ Builder :: getAccountsCurrency()(查看: – Peter

+0

您是否创建函数? – bafdurango