简体   繁体   中英

Laravel 5 - How to add multiple parameters to laravel array for db insert?

I have an array of 3 companies, which need to be inserted into the db but with 2 additional parameters added to them.

$companyList = [{"name": "apple", "founder": "steve"},
                {"name": "google", "founder": "larry"},
                {"name": "facebook", "founder": "mark"},
               ];

Need to append these 2 parameters for each company (issue is in this step):

$companyListFinal = [];
foreach ($companyList as $company) {
  $companyListFinal[] = array_add($company,['keyAppend1' => 'key 1 appended',
                                            'keyAppend2' => 'key 2 appended'];
}

The final step is to insert the company list with the appended values into the DB:

DB::table('companies')->insert($companyListFinal);

I can't seem to be able to append the 2 new parameters to create the final array to insert: $companyListFinal

What's the correct way to add the parameters to each company so they are all inserted at bulk?

您需要使用array_merge而不是array_add

Try using array_push() instead on array_add(). That should do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM