Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

morceaudebois's avatar

What would be your approach for a form with repeating data?

Hi! I'm trying to make a form where the user can add and delete repeating containers (Plans) like this: alt text

Once they're happy with what they have, they can save it (without having to reload the page). I'm struggling to find a logic that works for this with Livewire though, especially to add new plans. I have this that tries to add a new plan to the existing collection:

    public function addPlan() {
        $newPlan = new Plan();

        $newPlan->id = uniqid();
        $newPlan->title = 'New Plan Name';
        $newPlan->form_id = $this->form->id; 

        // Add the new plan to the collection
        $this->form->plans->push($newPlan);
    }

And it technically works and adds the new plan, but whenever I interact with the form in any other way, the collection goes back to its initial database state.

I spent a few hours on this and couldn't do anything about it. I can wire:ignore some parts of the form, but not all so the problem remains. I tried multiple things but always end up getting errors I can't fix, which leads me to believe there's a fundamental flow with how I'm approaching it.

If you have any idea, I would love some help! Thank you 😊

0 likes
1 reply
morceaudebois's avatar
morceaudebois
OP
Best Answer
Level 1

Alright I finally did it! 🎉

That vid helped me a ton: https://www.youtube.com/watch?v=l-kZyBoB24s

The main problem was kind of unrelated though, I was attributing uniqid() to $newPlan->id just to test things out, but that ID was an integer and couldn't have letters in it. It blocked the whole thing!

Please or to participate in this conversation.