How to add a key value pair to existing array in laravel
How to add a key value pair to existing array in laravel
With this article, we’ll look at some examples of how to address the "How to add a key value pair to existing array in laravel" problem.
-
Add a key value pair to array using Array helpers method
--PATH app\Http\Controllers\<YourController>.php//Before class definition use Illuminate\Support\Arr; //Inside controller's method $array = ['foo' => 'bar']; $array = Arr::add($array, 'price' , 100); //Before class definition use Illuminate\Support\Arr; //Real life example $product = Product::where('id', 1)->first(); $product = Arr::add($product, 'price' , 100);
This array method is the helper method which is used to add a new key value pair data to an existing array in laravel. You have to use
use Illuminate\Support\Arr;
before the class definition in your controller file and after that you can call the Array method.It adds the key and value to the array if the given key doesn't already exist in the array or is set to null. This code snippet has been tested on laravel 8.x and it’s working.
-
Add a new key and value pair to Array by specifying the key in brackets
$arr = array(1 => 10, 2 => 20); $arr["x"] = 30; $arr = array(1 => 10, 2 => 20, 'x' =>25); $arr["x"] = 30; $post = Post::where('id', 1)->first(); $post["new_key"] = 'New value'; print_r($post->toArray());
You can add a new key and value pair to an existing array by specifying the key in brackets and pass the value to this key using = operator. This method is very helpful if you have an array and you want to pass a new pair of data, or change the data by assigning the key and value to it.
It will change the data if the key already exists in it.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Rendering HTML from database table to view in Laravel
- How to change default timestamp fields name in Laravel
- Get Array of IDs from Eloquent Collection
- Symlink(): No such file or directory
- Route [password.request] not defined
- How to update record after save method in Laravel
- Pass value from controller to model in laravel
- Add class to body in laravel view
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Calculate age from date of birth in Laravel
- Get last record from table in laravel
- How to get the id of last record from collection object in laravel view
- How to access the nth object from Laravel collection object ?
- How to fill a column automatically while creating records in Laravel
- Insert values in pivot table dynamically in laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Laravel get all records with pagination
- Laravel form request validation
- Get 30 days older records from table in laravel
- How to automatically update the timestamp of parent model in Laravel
- Use withCount() to Calculate Child Relationship Records
- Send OTP using textlocal api in laravel
- Property [user] does not exist on this collection instance
- How to get random string in Laravel
- Convert input array to comma-separated string in laravel controller