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

vand's avatar
Level 1

How to insert all array value to DB with query builder laravel

hello, i have array with value like this :

^ array:3 [▼
1 => array:2 [▼
    "number" => "123"
    "desc" => "test1
]
6 => array:2 [▼
    "number" => "987"
    "desc" => "test2"
]
9 => array:2 [▼
    "number" => "123"
    "desc" => "test3"
]

my query builder :

foreach($data as $value) {
   DB::table('Data')->insert(
     ['number' => $value['number'], 'desc' =>$value['desc']
   );
}

its only inserted one array(first array), i need to pass all data to DB

0 likes
11 replies
Sinnbeck's avatar

Please format your code by adding ``` on the line before and after it

vand's avatar
Level 1

@MohamedTammam i need using query builder like insert with write $variable one by one, because my array and row DB have a different name

MohamedTammam's avatar

@vand I don't see anything wrong with your loop, are you sure it's only storing first value? Can you please show the whole code.

Sinnbeck's avatar

That should work. Is there more code you aren't showing us?

priyalaks's avatar

Can you please check your array . Not sure though if this the problem, your array has a missing quote ..

"desc" => "test1
maazin's avatar

If you need to do some formatting before you insert to DB then you could do array_map()

    $data = [
        ['number' => 123, 'des' => 'Hello'],
        ['number' => 456, 'des' => 'Hello again'],
    ];

    $data = array_map(fn ($row) => [
        'number' => $row['number'],
        'description' => $row['des'],
    ], $data);

    DB::table('Data')->insert($data);

You can also use laravel collection instead of array_map()

vand's avatar
Level 1

its my first foreach :

  $like = '123';
  foreach($list as $data) {
    $filter = array_filter($data, function ($item) use ($like) {
        if (stripos($item['number'], $like) !== false) {
           return true;
        }
        return false;
    });
  }

im tring to dd($filter) foreach and get array like this :

^ array:3 [▼
3=> array:2 [▼
    "number" => "123"
    "desc" => "test1"
]
7 => array:2 [▼
    "number" => "987"
    "desc" => "test2"
]
16 => array:2 [▼
    "number" => "123"
    "desc" => "test3"
]

im trying to insert to DB like this :

   DB::table('Data')->insert(
     ['number' => $value['number'], 'desc' =>$value['desc']
   );

and got error : indefined index number

so, i make a query builder in foreach like this :

foreach($data as $value) {
	dd($value);
   DB::table('Data')->insert(
     ['number' => $value['number'], 'desc' =>$value['desc']
   );
}

its can be stored to DB, but only one data , im trying to dd before insert and got array like this :

^ array:2 [▼
    "number" => "123"
    "desc" => "test1"
]

only show first array

Sinnbeck's avatar

@vand sorry but I'm struggling with following your code. First $data is each item in the first foreach, and then in the second foreach you use $data as all data?

Did you mean to do

  $like = '123';
    $filter = array_filter($list, function ($item) use ($like) {
        if (stripos($item['number'], $like) !== false) {
           return true;
        }
        return false;
    });
 

foreach($filter as $value) {
   DB::table('Data')->insert(
     ['number' => $value['number'], 'desc' =>$value['desc']
   );
}

Please or to participate in this conversation.