亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 Laravel API 資源數據是這樣的?

為什么 Laravel API 資源數據是這樣的?

PHP
弒天下 2023-10-01 15:47:22
我正在編寫 API 資源。我認為這是關于OOP的。在第一個代碼中,該fullName字段是空的,更準確地說,只是last_name來了。但在第二個代碼中,它完全按照我想要的方式工作。這是什么原因呢?第一的 public function toArray($request)    {        return [            'user_id'       => $this->id,            'email'         => $this->email,            'description'   => $this->description,            'first_name'    => $this->first_name,            'last_name'     => $this->last_name,            'full_name'     => $this->firstname . ' ' . $this->last_name,        ];    }第二public function toArray($request){    $firstName = $this->first_name;    $lastName  = $this->last_name;    $fullName  = $firstName . ' ' . $lastName;    return [        'user_id'       => $this->id,        'email'         => $this->email,        'description'   => $this->description,        'first_name'    => $this->first_name,        'last_name'     => $this->last_name,        'full_name'     => $fullName,    ];}
查看完整描述

1 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

你有$this->firstname第一個。應該是的$this->first_name。所以它應該看起來像這樣:

'full_name' => $this->first_name . ' ' . $this->last_name,


查看完整回答
反對 回復 2023-10-01
  • 1 回答
  • 0 關注
  • 92 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號