我在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
不在attributes
和original
之内。如果此时我在模型上运行toArray()
,则返回的数组没有gameName
属性,这是我需要的。任何帮助表示赞赏。
答案 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