Reputation: 79
i was wondering how can i insert an array of data into one table by using of 2 array like this in laravel model
$attributes =array('title','description');
$options =array('test','blahblahblah');
the table would be like
title test
description blahblahblah
so far i reach to this
$values = array(
array($attributes => $options),
);
but it says
Illegal array key type array less Arrays and objects can not be used as array keys.
any way i try has different error but the most of the errors is illegal offset type do you have any suggestion?
Upvotes: 1
Views: 691
Reputation: 111
You can try this:
First you have to combine array as following:
$tableFields = array('title', 'description');
$fieldValues = array('test', 'blahblahblah');
$newArr = array_combine($tableFields, $fieldValues);
Output :
array:2 [▼
"title" => "test"
"description" => "blahblahblah"
]
Then insert into table as following:
DB::table('table_name')->insert($newArr);
Upvotes: 1
Reputation: 8242
You can combine both arrays with array_combine. It will take an array for the keys and one array for the values.
$attributes = array('title', 'description');
$options = array('test', 'blahblahblah');
$values = array_combine($attributes, $options);
Result:
array:2 [▼
"title" => "test"
"description" => "blahblahblah"
]
Upvotes: 1