我在我的一个模型中有一个自定义函数。它看起来像这样:Laravel Eloquent:如何在保存前存储模型值?
public function newWithTeam($data, $team_id = false){
$levels = Permissions::get_levels();
$this->email = $data['email'];
$this->password = bcrypt($data['password']);
$this->username = $data['username'];
$this->save();
$profile = new Profile(['name' => $data['name'],'bio' => $data['bio']]);
$this->profile()->save($profile);
}
在这里,你可以看到我存储email
,password
和username
为对象的属性,命中save()
相反之前,我想做到这一点的一条线,东西像:
$this->store(['email' => $data['email], 'password' => $data['password], 'username' => $data['username']]);
$this->save();
我知道,create()
方法存在,但是当我用这个,下面一行 $this->profile()->save($profile);
不起作用PR operly。由于某种原因,我认为create()
功能与save()
不一样! 是否有与上述的store()
函数等价的函数?
哇!寻找这么久... – Ash