Laravel-makeVisible不会使隐藏的属性可见

问题描述 投票:0回答:1

我有以下代码:

$model = new coretable;
    log::info($model->all());
    $model = $model->makeVisible('id_coretable');
    log::info($model->all());

在我的流明日志中,得到以下结果:

[2020-02-26 10:14:19] local.INFO: [{"Internal_key":"TESTKEY_1"},{"Internal_key":"TESTKEY_2"},{"Internal_key":"TESTKEY_3"},{"Internal_key":"TESTKEY_4"},{"Internal_key":"TESTKEY_5"}]  
[2020-02-26 10:14:19] local.INFO: [{"Internal_key":"TESTKEY_1"},{"Internal_key":"TESTKEY_2"},{"Internal_key":"TESTKEY_3"},{"Internal_key":"TESTKEY_4"},{"Internal_key":"TESTKEY_5"}]

我希望"id_coretable"属性出现在log::info()的第二个输出中,但不是。这是为什么?这是coretable的模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CoreTable extends Model
{

    /**
   * The table associated with the model.
   *
   * @var string
   */
   protected $table = 'coretable';


  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'Internal_key'
  ];

  protected $hidden = [
    'id_coretable',
    'created_at',
    'updated_at'
  ];

  protected $primaryKey = 'id_coretable';



  /**
   * Many-To-Many relationship with User-Model.
   */
  public function extensiontable_itc()
  {
    return $this->hasOne('App\extensiontable_itc', 'coretable_id');
  }

  public function extensiontable_sysops()
  {
    return $this->hasOne('App\extensiontable_sysops', 'coretable_id');
  }

  public function inaccessibletable()
  {
    return $this->hasOne('App\inaccessibletable', 'coretable_id');
  }
}

我不知道为什么makeVisible()对效果没有任何影响。

php laravel eloquent orm lumen
1个回答
1
投票

您创建的初始模型对从all()功能接收的模型没有任何影响。这是带有初始$hidden数组的新模型的集合。

要更改显示的值,您必须在收到的集合上调用makeVisible

$model = new coretable;
log::info($model->all());
log::info($model->all()->makeVisible('id_coretable'));

也建议您静态地调用查询函数,这样您就无需创建初始模型:

log::info(coretable::all()->makeVisible('id_coretable'));
© www.soinside.com 2019 - 2024. All rights reserved.