如何在调用 Actor 关系数据时隐藏数据透视列数据调用时我想隐藏的列是包含“movie_id”和“person_id”的“pivot”
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');
}
}
返回的数据:
"actors": [
{
"job_title": "Actor",
"id": 1,
"name": "Jaquan Nicolas",
"profile_path": "asd",
"pivot": {
"movie_id": 1,
"person_id": 1
}
},
最佳答案
你需要定义:
protected $hidden = ['pivot'];
在您的 App\Person
模型上,而不是您的 Movie
模型。
关于php - laravel 隐藏枢轴数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41836113/