簡體   English   中英

Laravel模型屬性視圖

[英]Laravel model Property in view

我想在視圖中訪問模型類的靜態屬性

我努力了

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

您可以嘗試:

<?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'));
       }

如果您有任何疑問,請在下面發表評論。

您可以執行以下操作:

在模型中添加屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM