0

我正在将 laravel 8 与带有 Inertia.js Stack 的 jetstream 一起使用。我的数据库字段名称在snake_case 中,我在模板中使用了camelCase。我在配置文件中添加了测试、关于、显示名称和点等新字段。我可以成功保存所有输入的日期,但是当我刷新时,我可以看到显示名称没有带来数据。不知道为什么会这样,如果我在模板和数据库中将 display_name 更改为 snkae_case,那么它会将所有保存的信息带回显示配置文件。任何人都可以请指导我这个。

更新UserProfileInformation.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;

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', 'image', '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([
                'name' => $input['name'],
                'email' => $input['email'],
                'about'=> $input['about'],
                'display_name'=> $input['displayName'],
                'points'=> $input['points'],
                'test'=> $input['test'],
            ])->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();
    }
}

这是 UpdateProfileInformationForm.vue

 <div class="col-span-6 sm:col-span-4">
            <jet-label for="name" value="Name" />
            <jet-input id="name" type="text" class="mt-1 block w-full" v-model="form.name" autocomplete="name" />
            <jet-input-error :message="form.error('name')" class="mt-2" />
        </div>

        <!-- Email -->
        <div class="col-span-6 sm:col-span-4">
            <jet-label for="email" value="Email" />
            <jet-input id="email" type="email" class="mt-1 block w-full" v-model="form.email" />
            <jet-input-error :message="form.error('email')" class="mt-2" />
        </div>

        <!-- About -->
        <div class="col-span-6 sm:col-span-4">
            <jet-label for="about" value="About" />
            <jet-input id="about" type="text" class="mt-1 block w-full" v-model="form.about" autocomplete="about" />
            <jet-input-error :message="form.error('about')" class="mt-2" />
        </div>

        <!-- Display Name -->
        <div class="col-span-6 sm:col-span-4">
            <jet-label for="displayName" value="Display Name" />
            <jet-input id="displayName" type="text" class="mt-1 block w-full" v-model="form.displayName" autocomplete="displayName" />
            <jet-input-error :message="form.error('display_name')" class="mt-2" />
        </div>

        <!-- Total Points -->
        <div class="col-span-6 sm:col-span-4">
            <jet-label for="points" value="Points" />
            <jet-input id="points" type="text" class="mt-1 block w-full" v-model="form.points" autocomplete="points" />
            <jet-input-error :message="form.error('totalPoints')" class="mt-2" />
        </div>


        <!-- Test -->
        <div class="col-span-6 sm:col-span-4">
            <jet-label for="test" value="test" />
            <jet-input id="test" type="text" class="mt-1 block w-full" v-model="form.test" autocomplete="test" />
            <jet-input-error :message="form.error('test')" class="mt-2" />
        </div>

    </template>
4

1 回答 1

0

请编写一个 UpdateProfileInformationForm.vue 脚本 http/fetch/axios/form 或 Inertia 使用 post 或获取 dataForm 或 userForm Inertia。现在。->validateWithBag('updateProfileInformation'); 你使用它:

submit() {
  this.$inertia.post('/users', this.form)
},

但它会打开对话框响应或捕获 errorsBag / errors 道具

https://inertiajs.com/validation [如果使用惯性] 1

Axios ->validateWithBag('updateProfileInformation'); 是响应错误验证errorsBag/errors

可以在控制器中使用条件

$response->input('name')
return error||pass
于 2021-09-16T08:26:02.197 回答