1

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

3
  • 1
    Is the new data really ['5','BMW']? Or ['5' => 'BMW']? Commented Apr 12, 2018 at 21:24
  • ['5' => 'BMW']... Commented Apr 12, 2018 at 21:29
  • 4
    Use ->put(5, ‘BMW’) Commented Apr 12, 2018 at 21:30

1 Answer 1

3

You can use the collection like an array:

$cats[5] = 'BMW';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.