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

后端 未结 3 1598
时光说笑
时光说笑 2020-12-18 05:04

I use in my model code to get a relation

class User extends Authenticatable
{
    // ...
    public function extensions()
    {
        return $this->belo         


        
3条回答
  •  悲&欢浪女
    2020-12-18 05:27

    ->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']);
    });
    

提交回复
热议问题