[英]Laravel eloquent how to create relationship if it not exists?
I want to find or create user->settings relationship.我想查找或创建用户->设置关系。 I tried to use
$this->hasOne(UserSettings::class)->withDefault();
我尝试使用
$this->hasOne(UserSettings::class)->withDefault();
, but this method returns new relationship while need I to create and store relationship with default values in DB and then return it. ,但是这个方法返回新的关系,而我需要在数据库中创建和存储默认值的关系,然后返回它。 Is there any way to do so?
有什么办法吗?
route路线
Route::get('settings', function (Request $request) {
return $request->user()->load('settings');
});
relation关系
public function settings()
{
return $this->hasOne(UserSettings::class)->withDefault( // I want to save row with this values in DB if it not exists and return it after
[
'backup_password' => '',
'backup_email' => '',
'codeword' => '',
'security_notifications' => 0,
'password_changed_at' => null,
'two_step_authentication' => 0
]);
}
UPD UPD
I solved this problem like this, but I'm pretty sure that there is a better and cleaner solution我像这样解决了这个问题,但我很确定有更好更清洁的解决方案
if(!($this->hasOne(UserSettings::class)->exists())) {
$settings = new UserSettings();
$settings->user_id = Auth::id();
$settings->backup_password = '';
$settings->backup_email = '';
$settings->codeword = '';
$settings->security_notifications = 0;
$settings->password_changed_at = null;
$settings->two_step_authentication = 0;
$settings->save();
}
return $this->hasOne(UserSettings::class);
A cleaner way would be for example例如,更清洁的方法是
public function settings()
{
return $this->hasOne(UserSettings::class)->withDefault(function ($settings, $user) {
$settings->fill([
'backup_password' => '',
'backup_email' => '',
'codeword' => '',
'security_notifications' => 0,
'password_changed_at' => null,
'two_step_authentication' => 0,
]);
$user->settings()->save($settings);
});
}
Or even better if your default attributes are constant如果您的默认属性是恒定的,甚至更好
In your UserSettings
model在您的用户设置
UserSettings
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'backup_password' => '',
'backup_email' => '',
'codeword' => '',
'security_notifications' => 0,
'password_changed_at' => null,
'two_step_authentication' => 0,
];
public function settings()
{
return $this->hasOne(UserSettings::class)->withDefault(function ($settings, $user) {
$user->settings()->save($settings);
});
}
It would be best to have a separate method on the user model which calls the existing relationship or creates a new relationship and saves it to the database.最好对用户 model 有一个单独的方法,该方法调用现有关系或创建新关系并将其保存到数据库中。 For example:
例如:
public function settings()
{
return $this->hasOne(UserSettings::class)->withDefault(
UserSettings::defaultAttributes()
);
}
// This checks to see if the relationship already exists within
// the database and if it does not then creates a new relationship,
// saves it to the database with the default attributes which you
// could make on the UserSettings model and then returns the user Model
// with the new settings.
public function loadOrCreateSettings()
{
if ( ! $this->settings()->exists()) {
$this->settings()->create(UserSettings::defaultAttributes());
}
return $this;
}
Doing it this way means your routes are cleaner and you are letting the User model handle the logic.这样做意味着您的路线更干净,并且您让用户 model 处理逻辑。
Route::get('settings', function (Request $request) {
return $request->user()->loadOrCreateSettings();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.