Aleks Per
Aleks Per

Reputation: 1639

Laravel - push array to collection

I have this code where I try to grab all auth user categories:

$cats = Auth::user()->cats()->lists('title','id');

and I want to add new data to $cats so I write:

 $cats->push(['5','BMW']);

but I got:

    Collection {#459 ▼
  #items: array:2 [▼
    9 => "asd"
    10 => array:2 [▼
      0 => "5"
      1 => "BMW"
    ]
  ]
}

How I to change my code to get this result:

Collection {#459 ▼
  #items: array:2 [▼
    9 => "asd"
    5 => "BMW"
  ]
}

So how I can add the array to this collection? p.s. I need this format because I use select2 jquery plugin

Upvotes: 1

Views: 21166

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

You can use the collection like an array:

$cats[5] = 'BMW';

Upvotes: 3

Related Questions