orchidsoftware / platform

Orchid is a @laravel package that allows for rapid application development of back-office applications, admin/user panels, and dashboards.
https://orchid.software
MIT License
4.45k stars 657 forks source link

`getContent()` returns Array instead of Collection or Model when eager loading relations #2876

Open JeremieSchelb opened 3 months ago

JeremieSchelb commented 3 months ago

Describe the bug When a relation is eager-loaded, the getContent() method from the Orchid\Screen\AsSource trait returns an array instead of a collection or model.

To Reproduce Steps to reproduce the behavior:

  1. Eager load a relation (in either the Resource class or the Model class).
  2. In Tinker, call YourModel::first()->getContent('relationName').

Expected behavior The getContent() method should return a Collection or Model for consistency, especially when using it in a cell component with usingComponent().

Is there a specific reason for prioritizing an array return?

public function getContent(string $field)
{
    return Arr::get($this->toArray(), $field) // Try to get the field value from the object's array representation.
        ?? Arr::get($this->getRelations(), $field) // Try to get the field value from the object's relations.
        ?? $this->getAttribute($field);  // Try to get the field value from the object's attributes.
}

Server (please complete the following information):

tabuna commented 3 months ago

I believe this is related to the dot notation, but I'm open to suggestions if you'd like to improve this behavior.