我尝试在每次job_id时插入多个数组,当我尝试这个我的数组值输出为空值这是我的存储函数
$input
输出:
array:7 [▼
"date" => "2016-01-10",
"job_id" => "2",
"charge" => array:2 [▼
0 => "ch1",
1 => "ch2"
],
"category" => array:2 [▼
0 => "se1",
1 => "se2"
],
"amount" => array:2 [▼
0 => "100"
1 => "200"
]
]
完整代码:
$input = Input::all();
$charge = Input::get('charge');
$date = Input::get('date');
$job = Input::get('job_id');
$amount = Input::get('amount');
$cat = Input::get('category');
$id = \Auth::id();
$data = array();
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$key['charge'],
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$key['amount'],
'category'=>$key['category'],
];
}
dd($data);
当我试图退出时,我会得到这个
array:2 [▼
0 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
1 => array:6 [▼
"date" => "2016-01-10"
"charge" => null
"job_id" => "2"
"user_id" => 1
"amount" => null
"category" => null
]
]
我想要的是我想在我的数据库中插入数据任何人都建议我好主意?
从Laracast Check this link
回答这个问题答案 0 :(得分:1)
您的最终输出看起来是正确的,请尝试使用以下代码将$data
插入您的数据库:
Model::insert($data); // Eloquent
DB::table('table')->insert($data); // Query Builder
您应该使用$inputs['amount'][$key]
为数值构建数组,并使用$inputs['category'][$key]
为类别构建数组,如下所示:
foreach ($charge as $key=>$value ){
$data[] = [
'date' => $date,
'charge'=>$value,
'job_id'=>$job,
'user_id'=>$id,
'amount'=>$inputs['amount'][$key],
'category'=>$inputs['amount'][$key],
];
}
希望这有帮助。