Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kdm's avatar
Level 1

laravel hide pivot data

How do I hide pivot column data when calling actors relational data the columns I want to hide when calling are "pivot" that contains "movie_id" and "person_id"

//moviemodel.php class Movie extends Model { protected $table = 'movies';

protected $hidden = array('pivot'); // doesn't work

protected $fillable = [
    'adult',
    'tmdb_id',
    'imdb_id',
    'release_date',
    'original_language',
    'original_title',
    'title',
    'popularity',
    'backdrop_path',
    'poster_path',
    'runtime',
    'tagline',
    'trailer',
    'summary'
];

public function persons() {
    return $this->belongsToMany('App\Person', 'movies_pivot', 'movie_id', 'person_id');
}    

public function actors() {
    return $this->persons()->wherePivot('job_title', '=', 'Actor')->select('movies_pivot.job_title', 'persons.id', 'persons.name', 'persons.profile_path');
}

}

// data- return.php "actors": [{ "job_title": "Actor", "id": 1, "name": "Jaquan Nicolas", "profile_path": "asd", "pivot": { "movie_id": 1, "person_id": 1 } },

0 likes
3 replies
ramirors's avatar

Hi, I´m looking for the same, did you get it?

I know

protected $hidden = array('pivot');

but I need to hide on one method only, not entire model.

Thanks!!

1 like
vagkaefer's avatar

I made a foreach:

foreach ($profiles as  $profile) {
  foreach ($profile['professions'] as  $profession) {
    unset($profession['pivot']['profile_id']);
    unset($profession['pivot']['profession_id']);
  }
}

But this doesn't like good to me....

Please or to participate in this conversation.