
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
- InRandomOrder() method with example in laravel
- Get count of filter data, while return a small set of records
- Always load the relationship data with eager loading in Laravel
- How to validate form input data in laravel
- How to remove package from laravel
- Target class [admin] does not exist.
- Laravel save object to database
- Credit card validation in laravel
- How to pass external link in laravel blade to anchor tag
- Route prefix with auth middleware in laravel
- How to get last record from object collection in laravel
- How to get id of next record in laravel
- Get Array of IDs from Eloquent Collection
- Laravel 9 route group with controller
- Laravel specific table Migration
- How to create controller in laravel
- If no route matched route::fallback in laravel
- How to call Laravel route in jQuery
- Class "App\Http\Controllers\Auth\Verified" not found
- How to insert multiple rows in mysql using loop in laravel?
- Symlink(): No such file or directory
- Get products with number of orders in Laravel
- How to fetch single row data from database in laravel
- How to call model in blade laravel
- The openssl extension is required for SSL/TLS protection but is not available