title: How to insert an object (Model type object) into Collection Object in Laravel at specific index number?
body: <p>I have read Dayle Rees's <a href="https://daylerees.com/codebright/" rel="noreferrer">Code Bright</a> to know more about Eloquent <code>Collection</code>s used in Laravel. Did some other research as well but couldn't find the answer I was looking for.</p>
  
  <p>I want to insert an object (<code>Model</code> type object) into a <code>Collection</code> Object at a specific position.</p>
  
  <p>For example:</p>
  
  <p>This is the returned collection</p>
  
  <pre><code>Illuminate\Database\Eloquent\Collection Object
  (
      [0] =&gt; Attendance Object
          ([present_day] =&gt; 1)
  
      [1] =&gt; Attendance Object
          ([present_day] =&gt; 2)
  
      [2] =&gt; Attendance Object
          ([present_day] =&gt; 4) 
  
      [3] =&gt; Attendance Object
          ([present_day] =&gt; 5) 
  
  )
  </code></pre>
  
  <p>As you can see above <code>[present_day]</code> have a values ranging from <code>1 to 5</code>, but the value, <code>3</code> is missing in the sequence. Now, what I really want to do is, I want to explicitly put a new <code>Attendance Object</code> at the Collection Object's position of <code>[2]</code> 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: </p>
  
  <pre><code>Illuminate\Database\Eloquent\Collection Object
  (
      [0] =&gt; Attendance Object
          ([present_day] =&gt; 1)
  
      [1] =&gt; Attendance Object
          ([present_day] =&gt; 2)
  
      [2] =&gt; Attendance Object    // This is where new object was added.
          ([present_day] =&gt; 3) 
  
      [4] =&gt; Attendance Object
          ([present_day] =&gt; 4) 
  
      [5] =&gt; Attendance Object
          ([present_day] =&gt; 5) 
  
  )
  </code></pre>
  
  <p>I think there is some methods that will allow to do exactly like this if it was array. Since this is a <code>Collection</code>, I am not sure how to do it.</p>
  
  <p><strong>Note:</strong> 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 <code>Collection</code> object.</p>