dtmjl4427 2016-10-11 21:56
浏览 220
已采纳

Laravel 5 - 如何为raravel数组添加多个参数以进行数据库插入?

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?

  • 写回答

2条回答 默认 最新

  • douhao2153 2016-10-11 22:59
    关注

    You need to use array_merge instead of array_add

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标