1 month ago

Allow to load relations in an infinite circle: $appointment->client->appointments

Posted 1 month ago by RafaelMunoznl

I have the following model structure:

  • Appointment hasOne Client
  • Client hasMany Appointments

Now I have a calendar with all appointments foreach($appointments as $appointment)

  • Appointment10 (client 1)
  • Appointment11 (client 2)
  • Appointment 09 (client1)
  • etc

When I click on one of the coming appointments I open a modal (no a new view). That makes me to load all the data of each appointment in advance which I recognize is a huge performance stress, but it is a better UX.

The point here is that now in the Appointment details modal, I want to show all the appointments that the client has in the pass. His history: like this:

- Appointment 10 (February2020)
    - Client 1
        - Appointment 3 (December 2019)
        - Appointment 4 (November 2019)
        - Appointment 5 (October 2019)

In the modal I do this:

    {{ $appointment->client->full name }}

    foreach($appointment->client->appointments as $clientAppointments)
        {{ $clientAppointment->date }} // December 2019
        {{ $clientAppointment->time }}
        {{ $clientAppointment->notes }}

And I get an error:

Trying to get attribute of non object

It seems like Laravel does not allows to load an almost “infinite circle” here:


Is there any way to tell Laravel, that I need at least this one more level down of relations?

Please sign in or create an account to participate in this conversation.