Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mane's avatar
Level 1

Insert eloquent array with arrays

Hello, i have little issue, or problem with inserting data. So i have form with three fields that are going to one table, and that is working, but i have 4th field which is array, so its declared as name=member_id[]. Now first i insert in primary table, and then i need to insert all of those member_id[] in second table, its one to many, but i dont want to make model for that second table. So i want to make it plain with insert method. Now i made it like this.

      $group= Group::create([//stuff here]);
      foreach ($request->input('member_id') as $member) {
        DB::table('group_members')->insert(['member_id' => $member, 'group_id(fk)' => $group->id, 'custom'=>'0']);
      }

and that works. But i have problem, if there is lets say 15 of those members, then 15 queries are executed, i feel like that is not needed. So i found that i can make it like this

DB::table('group_members')->insert([
    ['member_id' => $member, 'group_id(fk)' => $group->id, 'custom'=>'0']//first,
    ['member_id' => $member, 'group_id(fk)' => $group->id, 'custom'=>'0']//second, etc...
]); 

and i hope that runs one query? So its better for resources. But i just cant manage to implement foreach like in my situation now, to make those arrays, and to place it in that insert. I tried to build string that looks like array, but that of course doesnt work. Is there any solution where it could be like

      $group= Group::create([//stuff here]);
      foreach ($request->input('member_id') as $member) {
        // make arrays here
      }

//and then

DB::table('group_members')->insert([
//place made arrays here
]); 

Thanks very much. Im out of ideas already :(

0 likes
4 replies
spekkionu's avatar

Just push an item onto an array that looks exactly like the array you were passing to the insert method in the loop.

mane's avatar
Level 1

Thanks for answering, could you be more specific? So i made it like this below

      foreach ($request->input('member_id') as $member) {
        $a = "['member_id' => $member, 'group_id' => $group->id, 'custom'=>'0']";
        array_push($arr, $a);
      }

pre insert i dd($arr) and it looks like

array:2 [▼
  0 => "['member_id' => 229, 'group_id' => 38, 'custom'=>'0']"
  1 => "['member_id' => 230, 'group_id' => 38, 'custom'=>'0']"
] 

So its basically one array as expected, and insertMany accepts each array separated. So this gives me error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `group_members` (`0`, `1`) values (['member_id' => 229, 'group_id' => 41, 'custom'=>'0'], ['member_id' => 230, 'group_id' => 41, 'custom'=>'0']))

Because it takes actuall arrays as value, and keys are 0 and 1 which should represent columns... Am i doing your idea wrong?

Thanks

spekkionu's avatar
Level 48

You are adding your rows as strings rather than arrays.
Remove the quotes on the outside.

mane's avatar
Level 1

Thanks, i got it working! :) Cheers.

Please or to participate in this conversation.