为什么Laravel API资源数据是这样来的?

Why does Laravel API Resource data come this way?

我正在编写 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,
        'email'         => $this->email,
        'description'   => $this->description,
        'first_name'    => $this->first_name,
        'last_name'     => $this->last_name,
        'full_name'     => $fullName,
    ];
}

您在第一个中有 $this->firstname 个。应该是$this->first_name。所以它应该是这样的:

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