Hi I want to insert this array using seeders into Laravel framework:
JavaScript
x
<?php
$countries = array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra");
?>
I read that I can do it this way:
JavaScript
<?php
DB::table('users')->insert($users);
?>
But I want to insert these countries in the column name of my table:
JavaScript
<?php
public function run()
{
DB::table('countries')->insert([
'name' => 'EE.UU',
]);
}
?>
I don’t want to do it manually, I want to insert the array of above completely.
Thank you so much.
Advertisement
Answer
Try this solution
JavaScript
$countries = array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra");
$insert = [];
foreach ($countries as $name) {
$insert[] = [
'name' => $name
];
}
DB::table('countries')->insert($insert);