I have 3 arrays with data from input form. Now I have to insert that data in a database.
My arrays are as following:
1st-NAME - a,b,c
2nd-ADDRESS- x,y,z
3rd-MOBILE-1,2,3
Now i have to add these in table like below:
How can I do this in Laravel? I have worked with 2 arrays using array_combine but how to do it for 3 arrays?
You can take advantage of Laravel collections and zip
method. Assuming you have Table
model your for table and added name
, address
and mobile
to $fillable
property of Table
model, you can do it like so:
$names = ['a', 'b', 'c'];
$addresses = ['x', 'y', 'z'];
$mobiles = [1, 2, 3];
collect($names)->zip($addresses, $mobiles)->map(function ($record) {
return [
'name' => $record[0],
'address' => $record[1],
'mobile' => $record[2],
];
})->each(function ($record) {
Table::create($record);
});