How to update the "date property" inside an alpine carousel?

Hi! I am building an announcement carousel with alpine. How to dynamically update the "date property" inside the carousel with each slide.

<div x-data="{
        currentIndex: 0,
        announcements: @js($announcements->map(fn($a) => [
            'id' => $a->id,
            'title' => $a->title,
            'description' => $a->announcement,
            'image' => asset('storage/2/hero1.jpg'),
            'date' => $a->created_at->format('M d, Y')
        ])),
        autoPlayInterval: null, 
        nextSlide() {
            this.currentIndex = (this.currentIndex + 1) % this.announcements.length;
        },
        prevSlide() {
            this.currentIndex = (this.currentIndex - 1 + this.announcements.length) % this.announcements.length;
        },
        autoPlay() {
            this.autoPlayInterval = setInterval(() => {
                this.nextSlide();
            }, 5000);
        },
        stopAutoPlay() {
            clearInterval(this.autoPlayInterval);
        },
        resumeAutoPlay() {
            this.autoPlay();
        }
    }"
    x-init="autoPlay()"
    class="relative rounded-lg bg-white p-2 py-5"
    @mouseenter="stopAutoPlay()" 
    @mouseleave="resumeAutoPlay()"
>
    <!-- Announcement Slides -->
    <div class="relative">
        <template x-for="(announcement, index) in announcements" :key="announcement.id">
            <div 
                x-show="currentIndex === index" 
                x-transition:enter="transition ease-in-out transform duration-500"
                x-transition:enter-start="translate-x-full opacity-0"
                x-transition:enter-end="translate-x-0 opacity-100"
                x-transition:leave="transition ease-in-out transform duration-500"
                x-transition:leave-start="translate-x-0 opacity-100"
                x-transition:leave-end="translate-x-full opacity-0"
                class="border-b-2 p-2"
            >
                <div class="flex justify-between p-2">
                    <div class="flex items-center">
                        <div class="flex items-center space-x-1 px-2 py-1 bg-yellow-400">
                            <h2 class="text-xl font-bold">Notice</h2>
                            <h4 x-text="announcement.date"></h4> 
                        </div>
                    </div>
                    <div class="flex items-center space-x-2">
                        <!-- Left Arrow -->
                        <button @click="prevSlide()" class="size-5 cursor-pointer">
                            <x-icon name="arrow-long-left" solid />
                        </button>
                        <!-- Right Arrow -->
                        <button @click="nextSlide()" class="size-5 cursor-pointer">
                            <x-icon name="arrow-long-right" solid />
                        </button>
                    </div>
                </div>

                <a :href="'/announcement/' + announcement.id">
                    <div class="p-2 grid lg:grid-cols-12">
                        <div class="lg:col-span-4 p-2">
                            <img :src="announcement.image" alt="Announcement Image" class="w-full rounded-lg">
                        </div>
                        <div class="lg:col-span-8 p-4 space-y-2">
                            <div class="text-center">
                                <div class="text-center font-bold" x-text="announcement.title"></div>
                                <div class="text-xs mt-1" x-text="announcement.date"></div> 
                            </div>
                            <div class="text-wrap" x-text="announcement.description"></div>
                        </div>
                    </div>
                </a>
            </div>
        </template>
    </div>
</div>
Prawin Member
Prawin
0
10
149
alex Member
alex
Moderator

Hey Prawin — what do you mean by 'update'? If you could give me some more detail about what you're trying to achieve I'd be happy to help!

Prawin Member
Prawin

In the announcement table there's a created_at filed which I am trying to fetch from the database inside the carousel using alpine.js but unfortunately "the date field (created_at)" for each meta data is not changing for each slide. The date remains static.

alex Member
alex
Moderator

Is that the only one that's not updating? And the description is, for example?

Prawin Member
Prawin

Yes, only the date is not updating for each set of meta data.

alex Member
alex
Moderator

Can I see how you've defined date in your model, or is it just a column?

Prawin Member
Prawin

protected $guarded = ['id'];

protected $hidden = ['created_at', 'updated_at'];
alex Member
alex
Moderator

I'm just wondering where the date property is coming from?

Edit: Just figured it out, sorry. Will respond with solution shortly after I've pulled down your code locally.

Haz Member
Haz
Moderator

protected $hidden = ['created_at', 'updated_at'];

How are you even getting the date if it's hidden?

Prawin Member
Prawin
Solution

I removed the $hidden property . It works. There were some issues with the created_at timestamp on my machine. Thank you for your responses, time and efforts.

Haz Member
Haz
Moderator

@Prawin Are you still facing the issue?

Would you mind creating a simplified repository (on GitHub for example) that I can pull down and take a look at? That way I can provide a better solution to your problem.