laravel 在jetstream更新表单中更新用户配置文件时,将用户的不同类型数据保存在不同的表中

egdjgwm8  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(128)

我在jetstream注册表单中添加了另外两个表BusinessBank沿着User。我在注册过程中一次成功地在所有这三个表中插入了数据。我的 app/Actions/Fortify/CreatedNewUser.php文件是:

<?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;
}

}
这里我在所有三个表中插入了数据**,但现在我想将此用户数据放在更新表单中进行更新。我不知道在哪里更新BusinessBank表沿着User表的数据。**
这里是 * 应用程序/操作/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();
}

}
在这里我看不到其他表的更新数据在哪里

7gcisfzg

7gcisfzg1#

你可以这样做:

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

 }
pn9klfpd

pn9klfpd2#

在用户模型中

protected $appends = ['bsns_name']; # add every field you want to access
.....

#create attribute
protected function getBsnsNameAttribute()
{
    return $this->business->bsns_name;
}

如果要访问另一个Model中的某个字段,请在User模态中创建一个属性,该属性提供您希望该用户使用的数据。
例如:
我想'business'是您与'Business'模型和'User'模型关系
然后在您的“update-profile-information-form.blade.php”中,您可以访问该属性。

<div class="col-span-6 sm:col-span-4">
      <x-jet-label for="bsns_name" value="{{ __('Bsns Name') }}" />
      <x-jet-input id="bsns_name" type="text" class="mt-1 block w-full" wire:model.defer="state.bsns_name" autocomplete="bsns_name" />
      <x-jet-input-error for="bsns_name" class="mt-2" />
</div>

将此应用于您需要的每个字段。

相关问题