Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果关系不存在,如何建立关系?_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 如果关系不存在,如何建立关系?

Php 如果关系不存在,如何建立关系?,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我要查找或创建用户->设置关系。我尝试使用$this->hasOne(UserSettings::class)->withDefault(),但此方法返回新的关系,而我需要在DB中创建并存储具有默认值的关系,然后返回它。有什么办法吗 路线 关系 public function settings() { return $this->hasOne(UserSettings::class)->withDefault( // I want to save row w

我要查找或创建用户->设置关系。我尝试使用
$this->hasOne(UserSettings::class)->withDefault(),但此方法返回新的关系,而我需要在DB中创建并存储具有默认值的关系,然后返回它。有什么办法吗

路线

关系

 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

我这样解决了这个问题,但我很确定有更好更干净的解决方案

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);

例如,更干净的方法就是

公共功能设置()
{
返回$this->hasOne(UserSettings::class)->withDefault(函数$settings,$user){
$settings->fill([
“备份密码”=>“”,
'备份电子邮件'=>'',
'码字'=>'',
“安全通知”=>0,
'密码\u已更改\u位于'=>null,
“两步认证”=>0,
]);
$user->settings()->save($settings);
});
}
如果默认属性为常量,则效果更好

在您的
用户设置中
型号

/**
*模型属性的默认值。
*
*@var数组
*/
受保护的$attributes=[
“备份密码”=>“”,
'备份电子邮件'=>'',
'码字'=>'',
“安全通知”=>0,
'密码\u已更改\u位于'=>null,
“两步认证”=>0,
];
公共功能设置()
{
返回$this->hasOne(UserSettings::class)->withDefault(函数$settings,$user){
$user->settings()->save($settings);
});
}

最好在用户模型上有一个单独的方法来调用现有关系或创建新关系并将其保存到数据库中。例如:

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;
}
这样做意味着您的路由更干净,并且让用户模型处理逻辑

Route::get('settings', function (Request $request) {
     return $request->user()->loadOrCreateSettings();
});
Route::get('settings', function (Request $request) {
     return $request->user()->loadOrCreateSettings();
});