kirschbaum-development / nova-inline-relationship

A package to present relationships as inline properties in Nova.
MIT License
198 stars 83 forks source link

Check if value is a instance of Collection #18

Closed gabrielkoerich closed 4 years ago

gabrielkoerich commented 5 years ago

Not sure if this is intended, but if $this->value is a Model instance, the pluck method will return all ids from the database, which is really slow here (>1M records).

brandonferens commented 4 years ago

@gabrielkoerich This completely makes sense. Good catch. However, it is possible that the value is not a model, and therefore it wouldn't have an accurate id. The id is needed for other field types and whatnot. Here is our suggestion for the fix:

if ($this->value instanceof Model) {
    $models = [$this->value->id];
} elseif ($this->value instanceof Collection) {
    $models = $this->value->pluck('id')->all();
}

$this->withMeta([
    ...
    'models' => $models ?? [],
    ...
]);

What are your thoughts on it?

brandonferens commented 4 years ago

We merged code in that fixes this issue in #32 in a more robust way plus resolved a secondary issue from #13. Thanks so much for the PR!