为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 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 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号