19

I have read Dayle Rees's Code Bright to know more about Eloquent Collections used in Laravel. Did some other research as well but couldn't find the answer I was looking for.

I want to insert an object (Model type object) into a Collection Object at a specific position.

For example:

This is the returned collection

Illuminate\Database\Eloquent\Collection Object
(
    [0] => Attendance Object
        ([present_day] => 1)

    [1] => Attendance Object
        ([present_day] => 2)

    [2] => Attendance Object
        ([present_day] => 4) 

    [3] => Attendance Object
        ([present_day] => 5) 

)

As you can see above [present_day] have a values ranging from 1 to 5, but the value, 3 is missing in the sequence. Now, what I really want to do is, I want to explicitly put a new Attendance Object at the Collection Object's position of [2] index number/position, thus by pushing the rest of the Attendance Object. I am really struggling to do this right. How can I do this to make above collection object to look like something as below:

Illuminate\Database\Eloquent\Collection Object
(
    [0] => Attendance Object
        ([present_day] => 1)

    [1] => Attendance Object
        ([present_day] => 2)

    [2] => Attendance Object    // This is where new object was added.
        ([present_day] => 3) 

    [4] => Attendance Object
        ([present_day] => 4) 

    [5] => Attendance Object
        ([present_day] => 5) 

)

I think there is some methods that will allow to do exactly like this if it was array. Since this is a Collection, I am not sure how to do it.

Note: I don't want to convert it this to array and do the insertion within array. For some reason, I want to have this output strictly in Collection object.

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Nirmalz Thapaz
  • 925
  • 4
  • 13
  • 28
  • 2
    The collection object has an add method, use that and then re-sort the collection like so; `$collection->sortBy(function($model){ return $model->present_day; });` This will then reorder the collection to what you are wanting. – Matt Burrow Dec 10 '14 at 09:20
  • I havent tried the solution yet. Just by reading your comment, I am sure it should work. Actually, quite a nice way to get around with my problem. – Nirmalz Thapaz Dec 10 '14 at 09:58
  • Ill add as an answer. – Matt Burrow Dec 10 '14 at 11:27

3 Answers3

20

To insert an item into a collection,refer to this answer; Answer

Basically, splits the collection, adds the item at the relevant index.


You can add the item to the Eloquent\Collection object with the add method;

$collection->add($item);  // Laravel 4

or

$collection->push($item); // Laravel 5 

Then you can reorder the collection using the sortBy method;

$collection = $collection->sortBy(function($model){ return $model->present_day; });

This will reorder the collection by your present_day attribute.


Note that the above code will only work if you are using Illuminate\Database\Eloquent\Collection. If you are using a plain Eloquent\Support\Collection, there is no add method.

Instead, you can use an empty array offset, the same as inserting a new element in a normal array:

$collection[] = $item;

This form also works on the Eloquent version of Collection.

Community
  • 1
  • 1
Matt Burrow
  • 10,477
  • 2
  • 34
  • 38
  • 1
    For everyone coming here and wondering that this doesn't work anymore [L5 and greater], try out `$collection->push($item);`. It appends an item at the collections end – manniL Jan 06 '17 at 22:08
  • 1
    I'm not sure why this is the accepted answer when there's no mention of the "index" – Andrew Feb 26 '17 at 13:29
  • Just important to mention that we also got `$collection->prepend()` and `$collection->merge()`, sometimes they're useful as well, as it is just `collect()` to create a new `Collection`. – giovannipds Mar 22 '17 at 22:30
0

The put method sets the given key and value in the collection:

$collection = collect(['product_id' => 1, 'name' => 'Desk']);

$collection->put('price', 100);

$collection->all();

// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]
Barungi Stephen
  • 759
  • 8
  • 13
0

Suppose you want to enter a collection item at position 4

$position = 4;

$top = $collection->splice(0,$position);

$bottom = $collection->splice($position);

$top->push($newItem);

$collection = $top->concat($bottom);
Snapey
  • 3,604
  • 1
  • 21
  • 19