laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

Fixed Undefined Key error in nested relationship

Open kohlerdominik opened this issue 3 years ago • 0 comments

Same Fix as https://github.com/lazychaser/laravel-nestedset/pull/547, which is already merged to v5

But this time directed to v6. @lazychaser is there a particular reason that v5 is the default branch? If i understand correctly, v6 is the current release and therefore should be default?

When using nested, optional relationships (which might be quite an edge case) there is a small possibility that the library is throwing an error.

It is correctly described in the comment above the problematic line, that The first model in the array is always the parent. This can be retrieved in most cases by index 0.

BUT, if the previously called filter in Illuminate/Database/Eloquent/Collection.php:227 happens to filter the model with index 0 (which is perfectly possible, as otherwise the filter would be unnecessary), then we have an Undefined array key 0 error.

Therefore, I propose to change the given implementation to use reset() to get the first model, as this truly returns the first model in the array rather than the model with index 0.

kohlerdominik avatar Feb 21 '22 13:02 kohlerdominik