prodigy7's avatar

Relationships are lost when converting to array

I'd a model with following relation:

customField [1:n] customFieldType [1:n] customFieldTypeListValues

I load the relations:

        $customField = \App\Models\CustomField::where('id', 1)->first();
        $customField->load('customFieldType.customFieldTypeListValues');

When I load a record with relations, dd shows me all relations:

When I'm using toArray(), I get following result:

array:9 [▼ // app/Http/Controllers/Document/CustomFieldController.php:71
  "id" => 2
  "custom_fieldable_type" => "App\Models\Document"
  "custom_fieldable_id" => 1
  "custom_field_type_id" => 2
  "value_string" => ""
  "created_at" => "16.09.2024 13:19:02"
  "updated_at" => "16.09.2024 14:03:26"
  "value" => array:1 [▶]
  "custom_field_type" => array:7 [▼
    "id" => 2
    "name" => "liste"
    "data_type" => "list_single"
    "is_required" => false
    "created_at" => "16.09.2024 13:18:58"
    "updated_at" => "16.09.2024 13:18:58"
    "archived_at" => null
  ]
]

As you can see, the relation custom_field_type_list_values is missing in the array. I inspect the framework code and all I see is that:

        return array_merge($this->attributesToArray(), $this->relationsToArray());

That irritated me because it should actually work. I rebuilt it by hand:

        $customField = \App\Models\CustomField::where('id', 1)->first();
        $customField->load('customFieldType.customFieldTypeListValues');
        $customFieldArray = array_merge($customField->attributesToArray(), $customField->relationsToArray());

Same result! Then I did this:

        $customField = \App\Models\CustomField::where('id', 1)->first();
        $customField->load('customFieldType.customFieldTypeListValues');
        $customFieldRelations = $customField->relationsToArray();
        $customFieldArray = array_merge($customField->attributesToArray(), $customFieldRelations);

And now the relations are loaded correctly:

array:9 [▼ // app/Http/Controllers/Document/CustomFieldController.php:73
  "id" => 2
  "custom_fieldable_type" => "App\Models\Document"
  "custom_fieldable_id" => 1
  "custom_field_type_id" => 2
  "value_string" => ""
  "created_at" => "16.09.2024 13:19:02"
  "updated_at" => "16.09.2024 14:03:26"
  "value" => array:1 [▶]
  "custom_field_type" => array:8 [▼
    "id" => 2
    "name" => "liste"
    "data_type" => "list_single"
    "is_required" => false
    "created_at" => "16.09.2024 13:18:58"
    "updated_at" => "16.09.2024 13:18:58"
    "archived_at" => null
    "custom_field_type_list_values" => array:1 [▼
      0 => array:6 [▼
        "id" => 1
        "custom_field_type_id" => 2
        "value" => "Wert 1"
        "created_at" => "16.09.2024 13:19:09"
        "updated_at" => "16.09.2024 13:19:09"
        "archived_at" => null
      ]
    ]
  ]
]

What's going wrong here? Am I making a mistake? Why is there a different behavior here when I call a method previous?

0 likes
1 reply
tisuchi's avatar

@prodigy7 What if you try to load the correct eager loading before calling toArray()?

For example:

\App\Models\CustomField::with('customFieldType.customFieldTypeListValues')->find(1);

Please or to participate in this conversation.