使用laravel更新两个表

时间:2017-08-17 10:46:42

标签: php laravel

您如何更新我的代码在两个表中的值,如下所示:

public function subscriberprofileupdate($ids, Request $request) {
    $profile = Subscriberinfo::find($ids); 
    $id = \Auth::user()->id;
    $user_id = \Auth::user()->user_id;
    $subscriber_id = \Auth::user()->subscriber_id;
    $inputteacher = $request->all();

    $profile->contact_person_firstname = $inputteacher['contact_person_firstname'];
    $profile->contact_person_lastname = $inputteacher['contact_person_lastname'];
    $profile->email = $inputteacher['email'];
    $profile->save();
    return Redirect('/subscriber/profile');
}

如果我可以在订阅者表中更新电子邮件,则意味着它也将使用user_id更新用户表。 我怎么能做到。

3 个答案:

答案 0 :(得分:0)

更好的方法是在您的订阅者表中添加一个名为 user_id 的新列,如果您在模型中给出了适当的关系,则可以获取数据。

答案 1 :(得分:0)

你可以简单地说:

User::where('id',$user_id)->update(['email' => $inputteacher['email']]);

它会更新Users表{/ 1}} users.id

$user_id表格电子邮件字段

答案 2 :(得分:0)

更新belongsTo关系时,您可以使用enter image description here方法。

例如:

$inputteacher = $request->all();
$userdetail = User::find(1);
$userdetail->firstname = $inputteacher['contact_person_firstname'];
$userdetail->lastname = $inputteacher['contact_person_lastname'];

$profile = Subscriberinfo::find($ids);
$profile->contact_person_firstname = $inputteacher['contact_person_firstname'];
$profile->contact_person_lastname = $inputteacher['contact_person_lastname'];
$userdetail->profiles()->save($profile);

//This will save not only a model, but also all of its relationships
$userdetail->push();