Refresh Relationship

I have a set of parent and child records i.e Adult::with(‘reminders’). In my form everything works apart from adding a new reminder.

The edit reminder works and it updates/saves as expected. The problem is when I add a new reminder, I need to refresh the foreach loop that shows all reminders.

I have tried an emit with a listener that has $refresh but nothing happens. I’m sure it is something simply I’m missing?

Yes I am using the latest version

Thanks

Hey, @760524mkfa00

Let’s assume you have a posts model, You can refresh the posts after adding new post like that:

public function save()
{
  // save actions
  $this->posts = $this->posts->refresh();
}
1 Like

Hi, this works great. I did make a slight change as my reminders table uses polymorphism, so I use the reminders form in multiple locations.

I take all the reminders and put them in their own public property. This allows me to use $reminders as $reminder in the form.

public Student $student;
public $reminders;
public Reminder $editing;

public function mount(Student $student)
    {

        $this->student = $student->load( 'reminders');;

        $this->reminders = $this->student->reminders;
    }

Then in my save method I have the following:

public function save()
    {
      // save actions
      $this->validate([
        'editing.reminder_body' => 'required|min:3|max:255',
        'editing.reminder_at' => 'required|date',
        'editing.completed_at' => 'nullable|date'
    ]);

    $this->student->reminders()->save($this->editing);

    $this->student = $this->student->refresh();

    $this->reminders = $this->student->reminders;

    // close bootstrap modal
    $this->dispatchBrowserEvent('closeModal');

    }

This all works really well now!

Thanks for your help.

I’m glade you solved your issue!

You are very welcome mate.