声明变量时无法访问 class 内的受保护变量

Unable to access protected variable within class when declaring variable

我是 laravel/php 的新手,希望有人能为我解答问题。然后,当我在这里使用 asset->setDescription 时,一切正常,但是一旦我取消注释 'protected $description',setDescription 方法就会停止工作。有谁知道为什么会这样?

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
    protected $fillable = [
        'type', 'title','origin',
    ];
    // protected $description;
    public function __construct($type, $title, $origin)
    {
        $this->setType($type);
        $this->setTitle($title);
        $this->setOrigin($origin);
    }
    // Setters
    public function setType($type){
        $this->type = $type;
    }
    public function setTitle($title)
    {
        $this->title = $title;
    }
    public function setOrigin($origin)
    {
        $this->origin = $origin;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
}

$type = $request->input('type');
$title = $request->input('title');
$origin = $request->input('origin');

// Create new asset
$asset = new Asset($type, $title, $origin);
$asset->setDescription('test');

$asset->save();```

Eloquent 不知道受保护的 属性。所有 Eloquent 模型属性都通过属性 属性 维护。如果您希望以这种方式完成设置值,请在 setDescription 方法中使用属性 属性:

public function setDescription($description)
{
    $this->attributes['description'] = $description;
}

有关详细信息,请参阅 mutators