Laravel 存储来自 for-each 循环的值并将其作为数组传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51054174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel store value from for-each loop and passed it as array
提问by Grace
I have two for-each loop inside my create view [Subject_id, Lead_id], I want to store the for-each value into my database using array approach, and I couldn't make it work can anyone amend my codes or point me to a proper way thanks.
我的创建视图 [Subject_id, Lead_id] 中有两个 for-each 循环,我想使用数组方法将 for-each 值存储到我的数据库中,但我无法使其工作,任何人都可以修改我的代码或指向我正确的方法谢谢。
Controller:
控制器:
public function store(Request $request)
{
//
$input = $request->all();
$items = array(['Subject_id','Lead_id']);
foreach($input as $inputs) {
$items[] = $inputs;
}
$scores = new Score();
$scores->Subject_id=$items['Subject_id'];
$scores->Lead_id=$items['Lead_id'];
$scores->save();
dd($request->all());
return redirect()->route('scores.create')->with('notif', 'Success.');
}
create view
创建视图
@foreach ($leads as $lead)
<tr>
<td>{{ $lead->student_name }}</td>
<td><input type="checkbox" class="checkbox" name="Lead_id[]" value="{{ $lead->id }}"></td>
</tr>
@endforeach
@foreach($subjects as $subject)
<label >
<input type="checkbox" name="Subject_id[]" value="{{ $subject->id }}">
{{ $subject->subject_name }}
</label>
@endforeach
回答by Jaskaran Singh
Try this code in your controller
在您的控制器中尝试此代码
public function store(Request $request)
{
$data = $request->all();
$leads = $data['Lead_id'];
$subject_ids = $data['Subject_id'];
//insert using foreach loop
foreach($leads as $key => $input) {
$scores = new Score();
$scores->Subject_id = isset($leads[$key]) ? $leads[$key] : ''; //add a default value here
$scores->Lead_id = isset($subject_ids[$key]) ? $subject_ids[$key] : ''; //add a default value here
$scores->save();
}
//insert using array at once
$rows = [];
foreach($leads as $key => $input) {
array_push($rows, [
'Subject_id' => isset($leads[$key]) ? $leads[$key] : '', //add a default value here
'Lead_id' => isset($subject_ids[$key]) ? $subject_ids[$key] : '' //add a default value here
]);
}
Score::insert($rows);
return redirect()->route('scores.create')->with('notif', 'Success.');
}
回答by Faizan Fayaz
Every time creating an instance of model in foreach loop in not an efficient way. You can do something like this
每次在 foreach 循环中创建模型实例都不是一种有效的方式。你可以做这样的事情
foreach($input as $inputs) {
$dataArray[] = [
'Subject_id' => $inputs['Subject_id'],
'Lead_id' => $inputs['Lead_id'],
];
}
DB::table('Score')->insert($dataArray);
You can manipulate the data as you want.
您可以根据需要操作数据。
回答by Ranjan Adhikari
Update your blade
更新您的刀片
@foreach ($leads as $lead)
{{ $lead->student_name }}
@foreach($subjects as $subject)
<input type="checkbox" name="Subject_id[$lead->id][]" value="{{ $subject->id }}">
{{ $subject->subject_name }}
@endforeach
@endforeach
This as your controller
这作为您的控制器
public function store(Request $request)
{
//
$subjects = $request->get('Subject_id');
foreach($subjects as $key=>$oneLeadOptions) {
foreach($oneLeadOptions as $option){
$scores = new Score();
$scores->Subject_id = $option;
$scores->Lead_id = $key;
$scores->save();
}
}
//To to other redirects logic here
}
try this
尝试这个