2017-01-24 129 views
0

如何隱藏透視列數據調用者當相關數據我想打電話時隱藏的列是「支點」包含「movie_id」和「PERSON_ID」laravel隱藏的樞軸數據

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 
    } 
}, 
+0

如果你正在獲取一個集合,你應該可以像這樣使用'except'方法:'$ movie-> actors-> except('pivot')' –

回答

0

嘗試添加該功能爲您的模型電影

public function toArray() 
{ 
    $attributes = $this->attributesToArray(); 
    $attributes = array_merge($attributes, $this->relationsToArray()); 
    unset($attributes['pivot']); 
    return $attributes; 
} 
0

您需要定義:

protected $hidden = ['pivot']; 

在您App\Person模式,而不是你的Movie模型。