Level 70
@prodigy7 What if you try to load the correct eager loading before calling toArray()?
For example:
\App\Models\CustomField::with('customFieldType.customFieldTypeListValues')->find(1);
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:
App\Models\CustomField {#1706 ▼ // app/Http/Controllers/Document/CustomFieldController.php:71
#connection: "mysql"
#table: "custom_fields"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:7 [▼
"id" => 2
"custom_fieldable_type" => "App\Models\Document"
"custom_fieldable_id" => 1
"custom_field_type_id" => 2
"value_string" => ""
"created_at" => "2024-09-16 13:19:02"
"updated_at" => "2024-09-16 14:03:26"
]
#original: array:7 [▶]
#changes: []
#casts: array:3 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: array:1 [▶]
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"customFieldType" => App\Models\CustomFieldType {#2302 ▼
#connection: "mysql"
#table: "custom_field_types"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:7 [▶]
#original: array:7 [▶]
#changes: []
#casts: array:5 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: array:2 [▶]
#observables: []
#relations: array:1 [▼
"customFieldTypeListValues" => Illuminate\Database\Eloquent\Collection {#2308 ▼
#items: array:1 [▼
0 => App\Models\CustomFieldTypeListValue {#2309 ▼
#connection: "mysql"
#table: "custom_field_type_list_values"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:6 [▼
"id" => 1
"custom_field_type_id" => 2
"value" => "Wert 1"
"created_at" => "2024-09-16 13:19:09"
"updated_at" => "2024-09-16 13:19:09"
"archived_at" => null
]
#original: array:6 [▶]
#changes: []
#casts: array:3 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:2 [▶]
#guarded: array:1 [▶]
}
]
#escapeWhenCastingToString: false
}
]
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:3 [▶]
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:5 [▶]
#guarded: array:1 [▶]
}
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?
Please or to participate in this conversation.