score:7
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;
score:1
If you are ok with the numeric keys being reindexed, then the accepted answer of using array_unshift()
works. If you want to maintain the original numeric keys (for example, if the keys correspond to the id of your table entries), then you could do as follows:
$statusCollection = \App\Status::lists('name', 'id');
$statusArray = $statusCollection->all();
$finalArray = array('0' => 'Select a value') + $statusArray;
More Answer
- Laravel lists function prepend value
- How to prepend item to laravel lists collection without reindexing?
- How to pass value on onclick function in laravel
- laravel - json_encodeing the returned value of trans function sent error when using single quote
- Laravel Model create function returns column with null value
- Cannot use laravel eloquent relationship latest() function to get last value because camel casing
- Using old() function with another default value for input in blade in laravel
- Laravel get value from my function in Model
- Route::get() function on laravel not returning proper value
- laravel angular not passing value to angular function
- Adding input query to route value for search function Laravel Scout
- Laravel variable value being lost when passing to a function
- Laravel Eloquent empty() function returns true for $request->url when there is a value
- How to call parent function value in child function in Laravel
- Handler Null Value in Looping Function ( Foreach) Laravel
- returning value from a function in middleware in laravel
- Laravel Value() function not returning the right value
- Laravel 5.4 how to passing id value from one function to another function?
- trans function in side lists function of laravel
- Laravel Schema Default Value Function
- Laravel editing a form, getting value for dropdown lists
- Trying to pass information to laravel ajax jquery function based on html declared value
- Trying to send multiple Key value inside with function for a Route: Laravel 5.2
- Using laravel constants as default value for arguments in a function
- How to pass array value to the function of javascript? PHP Laravel
- get the value of a foreach loop from laravel balde and pass it to javascript function
- Can't put data on input value from ajax success function returned by controller laravel
- Laravel How to get Helper function return value in controller as url path
- How to pass value to onclick function in Laravel
- validate all object value with the same validation function php laravel
More answer with same ag
- Laravel Validation Extend Not Firing
- Laravel findOrNew - How to redirect depending on what happened
- How to show inertia data to Vue Componets?
- Laravel 5 one instance, multiple identical subdomains
- Make a Carbon object out of request input
- Set Carbon Timezone only once
- Auth or auth-api middleware in api routes in laravel
- Laravel - How to Sum relative table with limit result?
- Laravel: one to many relationship issue
- Laravel 5.7 - Unable to run php artisan commands when shifted project to live
- Too few arguments to function App\Http\Controllers\TransactionsController::store(), 1 passed and exactly 2 expected
- multiple joins with the same table in eloquent
- How to fix bin/sh: can't create /etc/sysctl.conf: Permission denied when running docker-compose?
- Custom method instead of resource for Laravel routes
- How to make links, leading to themselves, unclickable in the whole project? Laravel 8
- SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value Laravel 8
- Connection refused when running Laravel artisan command with Docker
- How do I authenticate a user to access one page for a limited period of time in laravel
- How to concatenate a key in JSON response coming from API and display in VUE component
- Laravel 4 Blade markup and Ide Helper in PhpStorm
- Laravel 5.8 - collect method return me undefined index
- Updating models with dependency injection
- Eloquent Query Scope return Builder instead of Model when using phpunit
- Eager Load Dynamic Attribute in Laravel for jQuery Datatable
- Laravel Named route from ajax of View
- change font compile path in webpack mix laravel
- How to make a data array json javascript using controller laravel
- how to get nth row in foreach loop in laravel blade?
- How to addressing package view on Laravel 5.1
- Getting 419 Page expired on my return URL while integrating Jazzcash Payment Integration