Home > front end >  The filtered result I get by running the filter method on a laravel collection returns a new collect
The filtered result I get by running the filter method on a laravel collection returns a new collect

Time:09-18

To make it easier to understand the problem, I will hardcode the data that I am using the collection on and explain the problem.

Let us assume the following data structure in JSON format,

{
"shelters_with_linear_distances": [
    {
        "id": 3,
        "shelterName": "Third Shelter",
        "latitude": "5.0034000",
        "longitude": "70.1230000",
        "linear_distance": 3.1352984845527
    },
    {
        "id": 4,
        "shelterName": "Fourth Shelter",
        "latitude": "5.1413000",
        "longitude": "70.2250000",
        "linear_distance": 2.7850629146201
    },
    {
        "id": 5,
        "shelterName": "Fifth Shelter",
        "latitude": "5.2220000",
        "longitude": "70.1320000",
        "linear_distance": 2.6042789457753
    }
]
}

The following filter method is run on a collection format of 'shelters_with_linear_distance' in the above data structure and $minimum_distance_to_a_shelter is a dynamically calculated value that holds a data type of double.

$nearest_shelter = $shelters_with_linear_distances_from_user
    ->filter(function ($shelter, $key) use ($minimum_distance_to_a_shelter) {
        return $shelter['linear_distance'] == $minimum_distance_to_a_shelter;
    });

The problem here is if I send back the value returned by the filter method (which is the $nearest_shelter) as JSON to the frontend, in the postman I see the following output,

{
    "nearest_shelter": {
        "2": {       // <------------------------------------ I can not figure out from where this key '2' is coming from.
            "id": 5,
            "shelterName": "Fifth Shelter",
            "latitude": "5.2220000",
            "longitude": "70.1320000",
            "linear_distance": 2.6042789457753
        }
    }
}

The problem is I can not figure out from where the key I have pointed with an arrow in the above line of code is coming from. *) It is okay if that value '2' never changes so that in the later parts of code I can always access the $nearest_shelter as $nearest_shelter['2']. But the problem is, the value of that key changes depending on the data I am receiving from the db.

One time that value of the key was '1', then once I added some new records to the db it was '2'. Also this one other time there was no key marked as either '1' or '2' and the shelter I wanted was directly inside the collection.

Can someone please help me understand why this is happening and how to get rid of that since I want to access the value inside the $nearest_shelter in latter parts of the code and I do not want to get a key like that which I do not know the value of beforehand to access the $nearest_shelter later in the code.

(Project I am working on uses laravel 5.2)

Thanks.

CodePudding user response:

When you filter a collection, the index is preserved.

The "2" is because this element was the third (so index 2) in your original collection.

To fix this, just add ->values() after the filter:

$nearest_shelter = $shelters_with_linear_distances_from_user
    ->filter(function ($shelter, $key) use ($minimum_distance_to_a_shelter) {
        return $shelter['linear_distance'] == $minimum_distance_to_a_shelter;
    })->values();

This way the index will be reset and will start from 0, as usual.


From the documentation (for Laravel 5.2 as stated in your question) documentation:

The values method returns a new collection with the keys reset to consecutive integers

  • Related