简体   繁体   English

Laravel 无法从枢轴模型本身访问枢轴附加列

[英]Laravel can't access pivot additional column from pivot model itself

I have a pivot model called UserTask in which I have an accessor function:我有一个名为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);
    }

}

I specify the relation in my Task model like this:我在我的Task模型中指定了这样的关系:

class Task extends Model
{

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

I get the following error:我收到以下错误:

"message": "Undefined property: App\\Models\\UserTask::$completed", "message": "未定义的属性:App\\Models\\UserTask::$completed",

Anyone know why this is happening?有谁知道为什么会这样?

Try this accessor function:试试这个访问器功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM