【问题标题】:How do I add an object property to the object's attributes?如何将对象属性添加到对象的属性中?
【发布时间】:2015-11-15 15:59:54
【问题描述】:

我在 Eloquent 模型上有以下内容:

public function setGameName(\Game $game)
{
    $this->gameName = $game->find($this->gameId)->name;
}

当我在使用toArray() 之前在模型上使用dd() 时,我得到以下信息:

Promotion {#251 ▼
  #table: "Promotion"
  #primaryKey: "promotionId"
  #guarded: array:1 [▶]
  #game: null
  +gameName: "3Peaks"
  #connection: null
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:14 [▶]
  #original: array:14 [▶]
  #relations: array:5 [▶]
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

如您所见,gameNameattributesoriginal 之外。如果此时我在模型上运行toArray(),则返回的数组没有我需要的gameName 属性。任何帮助表示赞赏。

【问题讨论】:

    标签: php mysql laravel model eloquent


    【解决方案1】:

    设置在$this->attributes,蛇壳:

    public function setGameName(\Game $game)
    {
        $this->attributes['game_name'] = $game->find($this->gameId)->name;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2014-09-18
      • 2016-08-29
      • 1970-01-01
      相关资源
      最近更新 更多