Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index my laravel collection by a property

I have a laravel collection which I have sorted by the property 'name'.

{
"0":{"id":2,"name":"1","days":["2017-04-06","2017-04-07"]},
"1":{"id":3,"name":"2","days":["2017-04-08","2017-04-09"]},
"2":{"id":4,"name":"3","days":["2017-04-10","2017-04-11"]},
"6":{"id":10,"name":"4","days":["2017-04-12","2017-04-13"]},
"3":{"id":5,"name":"5","days":["2017-04-14","2017-04-15"]},
"4":{"id":6,"name":"6","days":["2017-04-16","2017-04-17"]},
"5":{"id":7,"name":"7","days":["2017-04-18","2017-04-19"]}
}  

However the index of this collection has not changed. The indexes are now 0,1,2,6,3,4,5. Is it possible to re-index the collection in the order of the sorted collection?

So what I would like to have is:

{
"0":{"id":2,"name":"1","days":["2017-04-06","2017-04-07"]},
"1":{"id":3,"name":"2","days":["2017-04-08","2017-04-09"]},
"2":{"id":4,"name":"3","days":["2017-04-10","2017-04-11"]},
"3":{"id":10,"name":"4","days":["2017-04-12","2017-04-13"]},
"4":{"id":5,"name":"5","days":["2017-04-14","2017-04-15"]},
"5":{"id":6,"name":"6","days":["2017-04-16","2017-04-17"]},
"6":{"id":7,"name":"7","days":["2017-04-18","2017-04-19"]}
}  

I tried using $collection->values(). But then I am left with:

[
{"id":2,"name":"1","days":["2017-04-06","2017-04-07"]},
{"id":3,"name":"2","days":["2017-04-08","2017-04-09"]},
{"id":4,"name":"3","days":["2017-04-10","2017-04-11"]},
{"id":10,"name":"4","days":["2017-04-12","2017-04-13"]},
{"id":5,"name":"5","days":["2017-04-14","2017-04-15"]},
{"id":6,"name":"6","days":["2017-04-16","2017-04-17"]},
{"id":7,"name":"7","days":["2017-04-18","2017-04-19"]}
]  
like image 866
Mazin Avatar asked May 23 '17 13:05

Mazin


People also ask

What is Laravel search() and how to use it?

If you are not familiar with Laravel Collections, then check out my Laravel Collections guide. Laravel search () is a built-in collections method that is used to search the collection for a given value. If the value is present in the collection, the key of the value is returned. If the value does not match any item, false is returned.

How to reset the index of a collection in Laravel?

As Laravel Collections docs says you can use values () on a collection for reseting the indexes: $resetedIndexesCollection = $yourcollection->values (); The values method returns a new collection with the keys reset to consecutive integers.

How to create a new collection in Laravel 8?

In laravel 8.x use transform , Unlike most other collection methods, transform modifies the collection itself. If you wish to create a new collection instead, use the map method. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research!

How to remove some fields from a collection in Laravel?

If you want to remove some fields from your collection, use reject method. Hope it helps you. Have a great day. Show activity on this post. In laravel 8.x use transform , Unlike most other collection methods, transform modifies the collection itself. If you wish to create a new collection instead, use the map method.


1 Answers

As Laravel Collections docs says you can use values() on a collection for reseting the indexes:

$resetedIndexesCollection = $yourcollection->values(); 

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

In your case is not working because you don't have a collection, you have a json, at first you need to json_decode it to create an array and then collect the array, now you have a collection and you can apply the values() method for reseting the indexes, like this:

collect(json_decode('{
                "0":{"id":2,"name":"1","days":["2017-04-06","2017-04-07"]},
                "1":{"id":3,"name":"2","days":["2017-04-08","2017-04-09"]},
                "2":{"id":4,"name":"3","days":["2017-04-10","2017-04-11"]},
                "3":{"id":10,"name":"4","days":["2017-04-12","2017-04-13"]},
                "4":{"id":5,"name":"5","days":["2017-04-14","2017-04-15"]},
                "5":{"id":6,"name":"6","days":["2017-04-16","2017-04-17"]},
                "6":{"id":7,"name":"7","days":["2017-04-18","2017-04-19"]}
                }'))->values();

This is the result:

Collection {#287 ▼
  #items: array:7 [▼
    0 => {#288 ▼
      +"id": 2
      +"name": "1"
      +"days": array:2 [▶]
    }
    1 => {#407 ▼
      +"id": 3
      +"name": "2"
      +"days": array:2 [▶]
    }
    2 => {#408 ▼
      +"id": 4
      +"name": "3"
      +"days": array:2 [▶]
    }
    3 => {#409 ▼
      +"id": 10
      +"name": "4"
      +"days": array:2 [▶]
    }
    4 => {#410 ▼
      +"id": 5
      +"name": "5"
      +"days": array:2 [▶]
    }
    5 => {#411 ▼
      +"id": 6
      +"name": "6"
      +"days": array:2 [▶]
    }
    6 => {#412 ▼
      +"id": 7
      +"name": "7"
      +"days": array:2 [▶]
    }
  ]
}
like image 184
Troyer Avatar answered Sep 24 '22 18:09

Troyer