lists()
returns a Collection
object. To add an item to the beginning of the collection, you can use the prepend
method:
$statusCollection = \App\Status::lists('name', 'id');
$statusCollection->prepend('select a value', 0);
Make sure you pass in a non-null key as the second parameter to prepend
, otherwise this method will end up renumbering your numeric keys. If you don't provide a key, or pass in null, the underlying logic will use array_shift
, which will renumber the keys. If you do provide a key, it uses an array union (+
), which should preserve the keys.
For another option, you can get the underlying item array using the all()
method and just do what you did before:
$statusCollection = \App\Status::lists('name', 'id');
$statusArray = $statusCollection->all();
// if you want to renumber the keys, use array_shift
array_unshift($statusArray, 'select a value');
// if you want to preserve the keys, use an array union
$statusArray = [0 => 'select a value'] + $statusArray;