laravel-nestedset
laravel-nestedset copied to clipboard
Fixed Undefined Key error in nested relationship
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 index0.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 anUndefined array key 0error.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.