Laravel 无法从数据透视模型本身访问数据透视附加列

Laravel can't access pivot additional column from pivot model itself

我有一个名为 UserTask 的枢轴模型,其中我有一个访问函数:

class UserTask extends Pivot implements HasMedia
{
    use HasMediaTrait;

    public function getCompletedAttribute()
    {
        return $this->getMedia()->isEmpty() && $this->completed;
    }



    public function task()
    {
        return $this->belongsTo(Task::class);
    }

}

我在 Task 模型中这样指定关系:

class Task extends Model
{

    public function users()
    {
        return $this->belongsToMany(User::class, 'user_task')->using('App\Models\UserTask')->withPivot('completed');
    }
}

我收到以下错误:

"message": "Undefined property: App\Models\UserTask::$completed",

有人知道为什么会这样吗?

试试这个访问函数:

public function getCompletedAttribute($value)
{
    return $this->getMedia()->isEmpty() && $value;
}