如何解决为什么Laravel API资源数据会以这种方式出现?
我正在编写API资源。我认为这与 OOP 有关。在第一个代码中,fullName
字段为空,更准确地说,只有last_name
出现。但是在第二个代码中,它的工作完全符合我的要求。这是什么原因?
FİRST
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,'full_name' => $fullName,];
}
解决方法
您的第一个有$this->firstname
。它应该是$this->first_name
。所以它应该像这样:
'full_name' => $this->first_name . ' ' . $this->last_name,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。