Gerne! Ich zeige dir, wie du in deinem bestehenden Code dynamisch ohne window.location.reload() arbeitest, mit einem zentralen Alpine Store und ohne Seiten-Neuladen.
1.) Alpine Store definieren
Füge am Anfang (z.B. direkt im <script>) einen globalen Store profile hinzu, basierend auf deinen Initialdaten:
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('profile', {
birthday: '{{ $formBirthday }}',
showBirthday: {{ $showBirthday }},
phone: '{{ $phone }}',
favoriteShip: '{{ $favoriteShip }}',
favoriteDestination: '{{ $favoriteDestination }}',
lastCruise: '{{ $lastCruise }}',
hobbies: '{{ $hobbies }}',
showAvatar: {{ $show_avatar }},
avatarUrl: '{{ $profile?->getAvatarUrlAttribute() ?? asset("avatar/default-avatar.jpg") }}',
update(data) {
Object.assign(this, data);
}
});
});
</script>
2.) Avatar-Bild an den Store binden (im Template)
Passe den <img>-Tag für Avatar an:
<img :src="$store.profile.avatarUrl" alt="{{ $user->name }}" class="profile-avatar w-full h-full object-cover" />
3.) profileFormComponent anpassen (für Toggle & automatisches Speichern)
Nutze direkt den Store in den Komponenten, z.B.:
function profileFormComponent() {
return {
init() {
// Watch auf den Store oder eigene Variable, hier einfach $store.profile.showAvatar
this.$watch('$store.profile.showAvatar', () => {
this.debouncedSave();
});
},
saveTimeout: null,
debouncedSave() {
clearTimeout(this.saveTimeout);
this.saveTimeout = setTimeout(() => this.saveProfile(), 700);
},
async saveProfile() {
try {
const payload = {
show_avatar: this.$store.profile.showAvatar ? 1 : 0,
birthday_visibility: this.$store.profile.showBirthday,
birthday: this.$store.profile.birthday,
telephone: this.$store.profile.phone,
favorite_ship: this.$store.profile.favoriteShip,
favorite_destination: this.$store.profile.favoriteDestination,
last_cruise: this.$store.profile.lastCruise,
hobbies: this.$store.profile.hobbies,
};
const response = await axios.post('/profile/update', payload, {
headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
});
if(response.data.success) {
this.$store.profile.update(response.data.profile);
// Kein reload mehr
} else {
alert('Fehler: ' + (response.data.message ?? 'Unbekannter Fehler'));
}
} catch (error) {
console.error(error);
alert('Fehler beim Speichern. Bitte versuche es erneut.');
}
}
}
}
Im Blade-Template kannst du die Alpine-Komponente so einsetzen:
<div x-data="profileFormComponent()">
<!-- z.B. Toggle, an Store gebunden -->
<x-urlex-toggle-yesNo
name="show_avatar"
:checked="$profile->show_avatar == 1"
label="Avatar anzeigen"
inputId="showAvatar"
x-model="$store.profile.showAvatar"
/>
</div>
4.) Im Modal die Bindung an den Store machen
Im Modal profileModalComponent bindest du ebenfalls an den Alpine Store:
function profileModalComponent() {
return {
get birthday() { return this.$store.profile.birthday },
set birthday(value) { this.$store.profile.birthday = value },
get showBirthday() { return this.$store.profile.showBirthday },
set showBirthday(value) { this.$store.profile.showBirthday = value },
get phone() { return this.$store.profile.phone },
set phone(value) { this.$store.profile.phone = value },
get favoriteShip() { return this.$store.profile.favoriteShip },
set favoriteShip(value) { this.$store.profile.favoriteShip = value },
get favoriteDestination() { return this.$store.profile.favoriteDestination },
set favoriteDestination(value) { this.$store.profile.favoriteDestination = value },
get lastCruise() { return this.$store.profile.lastCruise },
set lastCruise(value) { this.$store.profile.lastCruise = value },
get hobbies() { return this.$store.profile.hobbies },
set hobbies(value) { this.$store.profile.hobbies = value },
async saveProfile() {
try {
const payload = {
birthday: this.birthday,
birthday_visibility: this.showBirthday,
telephone: this.phone,
favorite_ship: this.favoriteShip,
favorite_destination: this.favoriteDestination,
last_cruise: this.lastCruise,
hobbies: this.hobbies,
show_avatar: this.$store.profile.showAvatar ? 1 : 0,
};
const response = await axios.post('/profile/update', payload, {
headers: {'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')}
});
if(response.data.success) {
this.$store.profile.update(response.data.profile);
closeModal('{{ $profileDataUpdate }}');
// Kein reload mehr
// Optional: Bestätigung anzeigen
// alert(response.data.message || 'Profil erfolgreich gespeichert');
} else {
alert('Fehler: ' + (response.data.message ?? 'Unbekannter Fehler'));
}
} catch(error){
console.error(error);
alert('Fehler beim Speichern. Bitte versuche es erneut.');
}
}
}
}
Im Blade-Template:
<div x-data="profileModalComponent()">
<x-input.text type="date" x-model="birthday" ... />
<x-input.select x-model="showBirthday" ... />
<x-input.text x-model="phone" ... />
<!-- usw. -->
</div>
5.) Avatar Upload aktualisieren
Im Avatar Upload kannst du nach Upload das neue Bild-URL direkt dem Store setzen:
async uploadImage() {
if (!this.selectedFile) {
alert('Bitte wählen Sie zuerst eine Bilddatei aus.');
return;
}
let formData = new FormData();
formData.append('avatar', this.selectedFile);
try {
const response = await axios.post('{{ route("profile.avatar.upload") }}', formData, {
headers: {
'Content-Type': 'multipart/form-data',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
}
});
if (response.data.success) {
alert('Bild erfolgreich hochgeladen!');
closeModal('{{ $uploadProfileAvatar }}');
Alpine.store('profile').avatarUrl = response.data.path;
} else {
alert('Fehler: ' + (response.data.message ?? 'Unbekannter Fehler'));
}
} catch (error) {
console.error(error);
alert('Upload fehlgeschlagen. Bitte versuche es erneut.');
}
}
6.) UI-Rendering überall mit $store.profile binden
Überall deine Anzeige, z.B.
<p x-text="$store.profile.phone"></p>
<p x-text="$store.profile.favoriteShip"></p>
<p x-text="$store.profile.hobbies"></p>
Alles zusammen:
- Anstatt einzelne Alpine-Komponenten mit ihren eigenen Status-Daten zu füllen,
- legst du einen globalen Store
$store.profile an,
- bindest im Template (und Alpine-Komponenten) alles an den Store,
- und nach POST auf das Backend schiebst du alle aktualisierten Daten in den Store,
- die UI aktualisiert sich sofort, ganz ohne
window.location.reload().
Falls du einzelne Codeausschnitte konkret zusammengebaut haben möchtest, sag Bescheid!
Ich kann dir auch ein komplettes Minimalbeispiel machen.