Nishanth
Nishanth

Reputation: 59

Update two tables using laravel

Hi how can i update a values in two tables my code as shown as below cases:

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

If i can update email in subscriber table means that it will also update on users table also by using the user_id. How can i make it.

Upvotes: 0

Views: 7036

Answers (3)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

When updating a belongsTo relationship, you may use the associate method.

For example:

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

Upvotes: 0

aaron0207
aaron0207

Reputation: 2333

You can simply:

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

It will update the Users table email field where users.id is $user_id

Upvotes: 3

Nikhil G
Nikhil G

Reputation: 2466

The better way is to add a new column named user_id in your subscriber table and you than you can fetch the data if you have given proper relation in your model.

Upvotes: 0

Related Questions