Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - how to makeVisible an attribute in a Laravel relation?

Tags:

laravel

I use in my model code to get a relation

class User extends Authenticatable
{
    // ...
    public function extensions()
    {
        return $this->belongsToMany(Extension::class, 'v_extension_users', 'user_uuid', 'extension_uuid');
    }
    // ...
}

The Extension has field password hidden.

class Extension extends Model
{
    // ...
    protected $hidden = [
        'password',
    ];
    // ...
}

Under some circumstances I want to makeVisible the password field.

How can I achieve this?

like image 880
AHeavyObject Avatar asked May 24 '17 20:05

AHeavyObject


1 Answers

->makeVisible([...]) should work:

$model = \Model::first();
$model->makeVisible(['password']);

$models = \Model::get();
$models = $models->each(function ($i, $k) {
    $i->makeVisible(['password']);
});

// belongs to many / has many
$related = $parent->relation->each(function ($i, $k) {
    $i->makeVisible(['password']);
});

// belongs to many / has many - with loading
$related = $parent->relation()->get()->each(function ($i, $k) {
    $i->makeVisible(['password']);
});
like image 152
DevK Avatar answered Oct 25 '22 22:10

DevK