Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel hide pivot data

Tags:

php

laravel

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"

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 returned:

"actors": [
{
    "job_title": "Actor",
    "id": 1,
    "name": "Jaquan Nicolas",
    "profile_path": "asd",
    "pivot": {
        "movie_id": 1,
        "person_id": 1
    }
},
like image 599
ONYX Avatar asked Jan 24 '17 18:01

ONYX


People also ask

How to hide pivot table columns?

Start Power Pivot in Microsoft Excel add-in and open a Power Pivot window. To hide an entire table, right-click the tab that contains the table and choose Hide from Client Tools. To hide individual columns, open the table for which you are hiding a column, right-click the column, and click Hide from Client Tools.


1 Answers

You need to define:

protected $hidden = ['pivot'];

On your App\Person model, not your Movie model.

like image 75
Eric Tucker Avatar answered Sep 20 '22 12:09

Eric Tucker