Rusurano's avatar

Advice Needed: Handling Nested Relationships in Livewire

Consider the Livewire component working with a model NoteBoard that has a One-To-Many relationship with model NoteGroup, which in turn has a One-To-Many relationship with another model Note. That is, one NoteBoard can have many NoteGroups, and one NoteGroup belongs to a single NoteBoard. In turn, one NoteGroup can have many Notes, and one Note can belong to a single NoteGroup.

A Livewire component is dedicated to managing such note board: adding, editing and removing note groups, as well as adding, changing and removing notes in each note group. The auxilary data displayed for each note depends on the currently selected fields. For example, consider the note having a category, which has a summary that is printed right after that category had been selected (by changing the <select> dropdown field inside a note form, for example).

Most solutions I have found online for this kind of setup have its flaws; some are noteworthy to present:

  • Livewire docs on Properties do not seem to get such case covered, probably due to its complexity.
  • The Parent-Child Form video by Laravel Daily suggests to work with raw arrays, but this becomes really clumsy really quick, especially in my scenario where I have my reasons (stated above) to bind to a model instance instead of duplicating it into the array.
  • A video by TALL Programmer shows how to work with collections, which is actually closer to what I want (effectively, a note board has a collection of note groups, and each note group has a collection of notes), though I'm not sure how to manage dependent fields on note configuration update (see my example about note category and its summary).

(I cannot include links yet, so I didn't get them here)

So, how do I set such a construction up properly, with the ability to add/delete new notes and note groups, without creating too much mess in a code? Consider all the models and their relations done and ready; the Livewire component is the actual problem.

Thanks in advance to everyone trying to help.

0 likes
0 replies

Please or to participate in this conversation.