The problem is that the filter() method does not update the underlying array of arrays. So the collection still represents an array, it is just that your array looks like this:
[ 4 => Object4, 7 => Object7, ]
While this is a perfectly valid array in PHP, it is not a valid array in JSON. Since this cannot be imagined as an array in JSON, it is converted to an object in JSON.
To get this correctly represented as an array in JSON, you just need to reinstall the Collection array. The correct method for this is the values() method. All he does is call array_values in the underlying array. This will cause the above array to be as follows:
[ 0 => Object4, 1 => Object7, ]
Now this is a valid numerical index array that JSON can understand and will treat as an array instead of an object.
While flatten may work for this particular case (your collection is a collection of Eloquent Models), this is actually not the right method and can lead to unintended consequences. In addition, it will execute a lot of additional logic that is not needed. It is best to use the correct method for what you are trying to achieve, and this is the values() method.
$obj = Cars::with('brand')->orderBy('id')->get(); return $obj->filter(function($value, $key) { return $value->display == true; }) ->values();
source share