微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel - 带有函数调用访问器的帮助文件

如何解决Laravel - 带有函数调用访问器的帮助文件

我有一个帮助文件,其中包含可从项目的所有部分访问我的功能

在这函数中,我有一个从模型调用访问器的函数,但在使用时出现错误

这里的功能

function placeholder_decode($placeholder,$documento)
{
    $variables['#document.protocol_number_view#'] = $documento->protocol_number_view;
    
    $string = json_decode(str_replace(array_keys($variables),array_values($variables),$placeholder));

    return $string[0]->value;

}

访问者是对 $documento->protocol_number_view调用

这是访问器的代码转化为模型代码

public function getProtocolNumberViewAttribute()
{
    $format = Configuration::select("value")->where("company_id",$this->attributes['company_id'])->where("name","prot_format")->get();

    if ($this->attributes['protocol_number'] != 0) {
        $number = placeholder_decode($format,$this);
    } else {
        $number = "";
    }
    return $number;
}

这是我得到的错误

[2021-03-10 20:27:54] local.ERROR: ErrorException: Undefined property: App\Document::$protocol_number_view in /var/www/protocol/app/helpers.PHP:15

这是 dd($documento) 到 placeholder_decode() 函数的结果:

App\Document {#939
  #table: "documents"
  #fillable: array:13 [
    0 => "company_id"
    1 => "date"
    2 => "type"
    3 => "subject"
    4 => "senderable_type"
    5 => "senderable_id"
    6 => "protocol_number"
    7 => "protocol_date"
    8 => "is_reserved"
    9 => "cancelled"
    10 => "created_user_id"
    11 => "updated_user_id"
    12 => "deleted_user_id"
  ]
  #dates: array:3 [
    0 => "date"
    1 => "protocol_date"
    2 => "deleted_at"
  ]
  #appends: array:1 [
    0 => "protocol_number_view"
  ]
  #connection: "MysqL"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:17 [
    "id" => 4
    "company_id" => 1
    "date" => "2020-11-03 19:51:14"
    "type" => "E"
    "subject" => "Richiesta Trofeo"
    "senderable_type" => "App\Address"
    "senderable_id" => 18
    "protocol_number" => 10
    "protocol_date" => "2021-03-10 18:45:50"
    "is_reserved" => 0
    "cancelled" => 0
    "created_user_id" => 1
    "updated_user_id" => 1
    "deleted_user_id" => null
    "created_at" => "2020-11-03 19:51:14"
    "updated_at" => "2021-03-10 18:45:50"
    "deleted_at" => null
  ]
  #original: array:17 [
    "id" => 4
    "company_id" => 1
    "date" => "2020-11-03 19:51:14"
    "type" => "E"
    "subject" => "Richiesta Trofeo"
    "senderable_type" => "App\Address"
    "senderable_id" => 18
    "protocol_number" => 10
    "protocol_date" => "2021-03-10 18:45:50"
    "is_reserved" => 0
    "cancelled" => 0
    "created_user_id" => 1
    "updated_user_id" => 1
    "deleted_user_id" => null
    "created_at" => "2020-11-03 19:51:14"
    "updated_at" => "2021-03-10 18:45:50"
    "deleted_at" => null
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #dateFormat: null
  #dispatchesEvents: []
  #observables: []
  #relations: array:2 [
    "company" => App\Company {#954
      #table: "companies"
      #fillable: array:4 [
        0 => "name"
        1 => "created_user_id"
        2 => "updated_user_id"
        3 => "deleted_user_id"
      ]
      #connection: "MysqL"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [
        "id" => 1
        "name" => "Federazione"
        "created_user_id" => null
        "updated_user_id" => null
        "deleted_user_id" => null
        "created_at" => "2020-10-08 18:45:47"
        "updated_at" => "2020-10-08 18:45:47"
        "deleted_at" => null
      ]
      #original: array:8 [
        "id" => 1
        "name" => "Federazione"
        "created_user_id" => null
        "updated_user_id" => null
        "deleted_user_id" => null
        "created_at" => "2020-10-08 18:45:47"
        "updated_at" => "2020-10-08 18:45:47"
        "deleted_at" => null
      ]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: array:1 [
        0 => "deleted_at"
      ]
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
    }
    "tags" => Illuminate\Database\Eloquent\Collection {#919
      #items: []
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
  #forceDeleting: false
  #queuedTags: []
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。