如何将对象属性添加到对象的属性?

时间:2015-08-21 16:06:24

标签: php mysql laravel model eloquent

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

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

在使用dd()之前在模型上使用toArray()时,我会得到以下内容:

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
}

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

1 个答案:

答案 0 :(得分:0)

将其设置为var fs = require('fs'); var bc = require('bcrypt-nodejs') fs.readFile('users.txt',function(err,data){ var x = []; if(err) throw err; var buf = new Buffer(data); x = buf.toString(); x = JSON.parse(x); //console.log(x); for(var a in x){ x[a].password=createHash(x[a].password); } x = JSON.stringify(x); fs.writeFile('users.txt',x,callback()); //or just push directly to db? }); var createHash = function(password){ return bc.hashSync(password, bc.genSaltSync(10), null); } function callback(){ console.log('finished'); } ,蛇形:

$this->attributes