Search code examples
phplaravellaravel-8laravel-jetstream

Save user's different type data in different tables while updating userprofile in jetstream update form


I added two more tables Business and Bank along with User in jetstream registration form. I inserted data successfully in all these three tables at once at registration process. my app/Actions/Fortify/CreatedNewUser.php file is:

<?php

 namespace App\Actions\Fortify;

 use App\Models\Team;
 use App\Models\User;
 use App\Models\Business;
 use App\Models\Bank;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Validator;
 use Laravel\Fortify\Contracts\CreatesNewUsers;
 use Laravel\Jetstream\Jetstream;

 class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

/**
 * Create a newly registered user.
 *
 * @param  array  $input
 * @return \App\Models\User
 */
public function create(array $input){

$user = User::create([
 
    'user_id' => $input['user_id'],
    'username' => $input['username'],
    'name' => $input['name'],
    'lname' => $input['lname'],
    'email' => $input['email'],
    'phone' => $input['phone'],
    'cell' => $input['cell'],
    'persnl_add1' => $input['persnl_add1'],
    'persnl_add2' => $input['persnl_add2'],
    'city' => $input['city'],
    'province' => $input['province'],
    'country' => $input['country'],
    'web' => $input['web'],
    'password' => Hash::make($input['password']),
]);

Business::create([
    'user_id' => $input['user_id'],
    'bsns_name' => $input['bsns_name'],
    'bsns_add1' => $input['bsns_add1'],
    'bsns_add2' => $input['bsns_add2'],
    'bsns_city' => $input['bsns_city'],
    'bsns_province' => $input['bsns_province'],
    'bsns_country' => $input['bsns_country'],
    ]);

Bank::create([
    'user_id' => $input['user_id'],
    'branch_code' => $input['branch_code'],
    'bank_name' => $input['bank_name'],
    'acc_no' => $input['acc_no'],
    'acc_title' => $input['acc_title'],
    ]);

    return $user;
}

}

Here I inserted data in all three tables but now I want to put this user data in update form to update. I have no idea where to update data of Business and Bank tables too along with User table.

here is the app/Actions/Fortify/UpdateUserProfileInformation.php

<?php

namespace App\Actions\Fortify;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Illuminate\Support\Facades\Hash;

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
/**
 * Validate and update the given user's profile information.
 *
 * @param  mixed  $user
 * @param  array  $input
 * @return void
 */
public function update($user, array $input)
{
    Validator::make($input, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
        'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
    ])->validateWithBag('updateProfileInformation');

    if (isset($input['photo'])) {
        $user->updateProfilePhoto($input['photo']);
    }

    if ($input['email'] !== $user->email &&
        $user instanceof MustVerifyEmail) {
        $this->updateVerifiedUser($user, $input);
    } else {
        $user->forceFill([
            'username' => $input['username'],
            'name' => $input['name'],
            'lname' => $input['lname'],
            'email' => $input['email'],
            'phone' => $input['phone'],
            'cell' => $input['cell'],
            'persnl_add1' => $input['persnl_add1'],
            'persnl_add2' => $input['persnl_add2'],
            'city' => $input['city'],
            'province' => $input['province'],
            'country' => $input['country'],
            'web' => $input['web'],
        ])->save();
    }
}

/**
 * Update the given verified user's profile information.
 *
 * @param  mixed  $user
 * @param  array  $input
 * @return void
 */
protected function updateVerifiedUser($user, array $input)
{
    $user->forceFill([
        'name' => $input['name'],
        'email' => $input['email'],
        'email_verified_at' => null,
    ])->save();

    $user->sendEmailVerificationNotification();
}

}

Here I can't see to where update data of those other tables


Solution

  • well , you can do like this:

    public function update($user, array $input)
    {
     // your function is fine ,dont change any thing but add this lines:
    
    //find your model objects; 
    $business = Business::find([user_id=>$user->user_id]);
    $bank= Bank::find([user_id=>$user->user_id]);
    
    // start adding data to your first model:
    $business->bsns_name =  $input['bsns_name'],
    $business->bsns_add1=  $input['bsns_add1'],
      .
      .
      .
    
       $business->save();
    
    
    
    
    // and do this with your $bank object too;
    
    $bank->bank_name=  $input['bank_name'],
      .
      .
      .
    
       $bank->save();
    
     }