简体   繁体   English

无法在 Laravel 5.8 中以一对多关系更新表

[英]Unable to update tables in one-to-many relationship in laravel 5.8

I am new to laravel and making authentication system where a user can login using facebook,twitter or simple by email我是 laravel 的新手,正在制作身份验证系统,用户可以使用 facebook、twitter 或简单的电子邮件登录

i have made two tables我做了两张桌子

1-user 1-用户

2-user_social_accounts 2-user_social_accounts

There is one-to-many relationship between them(a user can have many social accounts).它们之间是一对多的关系(一个用户可以拥有多个社交账号)。

Model details are型号详情是

User model用户模型

public function user_social_accounts()
    {
        return $this->hasMany('App\UserSocialAccount');
    }

UserSocialAccount model UserSocialAccount 模型

public function user()
    {
        return $this->belongsTo('App\User');
    }

now here is logic现在这是逻辑

Case1-情况1-

-if user first signup using email then a new record will be created -If user login using facebook then his email will store into user table and facebook data will be store in user_social_accounts table -if user again login using the same email with twitter then a new record will be created in user_social_accounts - 如果用户第一次使用电子邮件注册,那么将创建一个新记录 - 如果用户使用 facebook 登录,那么他的电子邮件将存储到user表中,而 facebook 数据将存储在user_social_accounts表中 - 如果用户再次使用与 twitter 相同的电子邮件登录,则将在user_social_accounts创建新记录

Case2-案例2-

-If user first login using social site and his record does not exist with that email, then at first his email will be stored in user table and then facebook data will be stored in user_social_accounts table -如果用户第一次使用社交网站登录并且他的记录不存在该电子邮件,那么首先他的电子邮件将存储在user表中,然后facebook数据将存储在user_social_accounts表中

-if user then again signup using same email (simple login) then i would update that email record in databse(if exist). -如果用户然后再次使用相同的电子邮件注册(简单登录),那么我将更新数据库中的电子邮件记录(如果存在)。

So it is like if does not exist then create and if exist then update(based on email).所以就像如果不存在则创建,如果存在则更新(基于电子邮件)。

I am very close but stuck from last 1 day我非常接近,但从最后 1 天开始卡住了

Here is my Auth controller这是我的 Auth 控制器

public function facebook_login(Request $request) {    
    $validator = Validator::make($request->all(), [ 
      'first_name' => 'required',
      'last_name' => 'required',
      'photo' => 'required',
      'email' => 'required|email',
      'facebook_id' => 'required',
      'fcm_token' => 'required'
    ]);   

    if($validator->fails()){
      $status  = 0;
      $data['message'] = $validator->errors();
      return response()->json(['status' => $status,'data' => $data], 401);
    }   

    $input = $request->all();
    $user = User::updateOrCreate(['email' => $input['email']],$input);

    $user = User::find($user['id']);
    $account_details = new UserSocialAccount();
    $account_details->first_name = $input['first_name'];
    $account_details->last_name = $input['last_name'];
    $account_details->photo = $input['photo'];
    $account_details->provider_id = $input['facebook_id'];
    $account_details->provider_name = 'facebook';
    $account_details->fcm_token = $input['fcm_token'];
    $input['provider_name'] = 'facebook';
    //the following line is saving new records and not updating them
    $user->user_social_accounts()->save($account_details);

}

the following line is saving new records and not updating them以下行是保存新记录而不是更新它们

$user->user_social_accounts()->save($account_details);

Please guide me I am worried.Thanks and advance sorry for any silly question.请指导我,我很担心。感谢并提前为任何愚蠢的问题道歉。

You are currently creating a new record and then saving it, so this is the expected behavior.您当前正在创建一个新记录然后保存它,所以这是预期的行为。 You will need to approach this differently.您将需要以不同的方式处理此问题。 In my opinion, the relationship just gets in the way in this case.在我看来,这种关系只是在这种情况下妨碍了。

Instead, I would do it like this and refer to the relationship manually:相反,我会这样做并手动引用关系:

// ... previous code 

$input = $request->all();

$user = User::updateOrCreate([
    'email' => $input['email']
],$input);

$account_details = UserSocialAccount::updateOrCreate([
    'user_id' => $user->id, // refer to the relationship manually
    'provider_name' => 'facebook'
],[
    'first_name' => $input['first_name'],
    'last_name' => $input['last_name'],
    'photo' => $input['photo'],
    'provider_id' => $input['facebook_id'],
    'fcm_token' => $input['fcm_token'],
])

// then if you need the new relationship loaded in your response
$user->load('user_social_accounts');

// do more stuff...

EDIT : As a post script, I will add that any time you are performing multiple related database inserts like this, you should really consider using transactions.编辑:作为一个后期脚本,我会补充说,每当你像这样执行多个相关的数据库插入时,你真的应该考虑使用事务。 You can find more information at the link below.您可以在下面的链接中找到更多信息。

https://laravel.com/docs/5.8/database#database-transactions https://laravel.com/docs/5.8/database#database-transactions

I hope this helps and good luck!我希望这个帮助能祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM