在Laravel中填写数据库中的另一个列?

I have a 'firstname' field and a 'lastname' field and I have 3 columns in my table 'firstname', 'lastname' and 'fullname'.

I would like the 'fullname' field to be built automatically from 'Firstname' and 'Lastname'.

I tried this :

public function getFullName()
{
    if ($this->fullname != '') {
        return $this->fullname;
    }

    if ($this->fullname == '' )
    {
        $firstname = $this->firstname;
        $lastname = $this->lastname;

        $this->title = $firstname . ' ' . $lastname;

        return $this->title;

    }
}

Every time I send my form, it tells me that the 'fullname' field must be filled in.

I've zero knowledge about laravel, but I believe you should call a save procedure if you want to store fullname attribute in database.

if ($this->fullname == '' )
{
     $firstname = $this->firstname;
     $lastname = $this->lastname;

     $this->fullname = $firstname . ' ' . $lastname;
     //call a save operation
     return $this->fullname;
}

you are missing $this->save(); you need to store fullname in database before returning fullname

if ($this->fullname == '' )
    {
        $firstname = $this->firstname;
        $lastname = $this->lastname;
        $this->title = $firstname . ' ' . $lastname;
         $this->save();
        return $this->title;
    }