Skip to content

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

@shabaz-ejaz

Description

@shabaz-ejaz
  • Laravel Version: 5.5.35
  • PHP Version: 7.0.10
  • Database Driver & Version: MySQL 5.7.14

Description:

I have a pivot model called UserTask in which I have an accessor function:

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:

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",

Anyone know why this is happening?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions