Hi,
I've been playing around with Jetstream, VueJs & Interia. I tried to add an additional field(addressl1) to my users profile however when I load the profile page the field is there but the current database value is missing. I'm sure I'm doing something simple wrong as I've never used Interia Js before. My code is as follows:
updateProfileInformation.vue
<template>
<jet-form-section @submitted="updateProfileInformation">
<template #title>
Profile Information
</template>
<template #description>
Update your account's profile information and email address.
</template>
<template #form>
<!-- Profile Photo -->
<div class="col-span-6 sm:col-span-4" v-if="$page.jetstream.managesProfilePhotos">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
ref="photo"
@change="updatePhotoPreview">
<jet-label for="photo" value="Photo" />
<!-- Current Profile Photo -->
<div class="mt-2" v-show="! photoPreview">
<img :src="$page.user.profile_photo_url" alt="Current Profile Photo" class="rounded-full h-20 w-20 object-cover">
</div>
<!-- New Profile Photo Preview -->
<div class="mt-2" v-show="photoPreview">
<span class="block rounded-full w-20 h-20"
:style="'background-size: cover; background-repeat: no-repeat; background-position: center center; background-image: url(\'' + photoPreview + '\');'">
</span>
</div>
<jet-secondary-button class="mt-2 mr-2" type="button" @click.native.prevent="selectNewPhoto">
Select A New Photo
</jet-secondary-button>
<jet-secondary-button type="button" class="mt-2" @click.native.prevent="deletePhoto" v-if="$page.user.profile_photo_path">
Remove Photo
</jet-secondary-button>
<jet-input-error :message="form.error('photo')" class="mt-2" />
</div>
<!-- Name -->
<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>
<div class="col-span-6 sm:col-span-4">
<jet-label for="addressl1" value="Address Line 1" />
<jet-input id="addressl1" type="text" class="mt-1 block w-full" v-model="form.addressl1" autocomplete="addressl1" />
<jet-input-error :message="form.error('addressl1')" 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>
</template>
<template #actions>
<jet-action-message :on="form.recentlySuccessful" class="mr-3">
Saved.
</jet-action-message>
<jet-button :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
Save
</jet-button>
</template>
</jet-form-section>
</template>
<script>
import JetButton from './../../Jetstream/Button'
import JetFormSection from './../../Jetstream/FormSection'
import JetInput from './../../Jetstream/Input'
import JetInputError from './../../Jetstream/InputError'
import JetLabel from './../../Jetstream/Label'
import JetActionMessage from './../../Jetstream/ActionMessage'
import JetSecondaryButton from './../../Jetstream/SecondaryButton'
export default {
components: {
JetActionMessage,
JetButton,
JetFormSection,
JetInput,
JetInputError,
JetLabel,
JetSecondaryButton,
},
props: ['name', 'addressl1', 'email'],
data() {
return {
form: this.$inertia.form({
'_method': 'PUT',
name: this.name,
addressl1: this.addressl1,
email: this.email,
photo: null,
}, {
bag: 'updateProfileInformation',
resetOnSuccess: false,
}),
photoPreview: null,
}
},
methods: {
updateProfileInformation() {
if (this.$refs.photo) {
this.form.photo = this.$refs.photo.files[0]
}
this.form.post('/user/profile-information', {
preserveScroll: true
});
},
selectNewPhoto() {
this.$refs.photo.click();
},
updatePhotoPreview() {
const reader = new FileReader();
reader.onload = (e) => {
this.photoPreview = e.target.result;
};
reader.readAsDataURL(this.$refs.photo.files[0]);
},
deletePhoto() {
this.$inertia.delete('/user/profile-photo', {
preserveScroll: true,
}).then(() => {
this.photoPreview = null
});
},
},
}
</script>
UserUpdateProfileInformation.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'],
'addressl1' => ['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'],
'addressl1' => $input['addressl1'],
'email' => $input['email'],
])->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([
'addressl1' => $input['addressl1'],
'name' => $input['name'],
'email' => $input['email'],
'email_verified_at' => null,
])->save();
$user->sendEmailVerificationNotification();
}
}
Thanks,