当我尝试将多个记录保存到数据库时,我收到此错误
未定义的偏移量:2
我的商店控制器
public function store(Request $request)
{
$course = Course::create($request->all());
$lessons = $request->except(['_token','_method']);
for($x = 0;$x <= count($lessons); $x++)
{
$lesson = CourseClass::create([
'course_id' => $course->id,
'class_name' => $lessons['class_name'][$x],
'class_desc' => $lessons['class_desc'][$x],
'video' => $lessons['video'][$x],
'files' => $lessons['files'][$x],
'free' => $lessons['free'][$x],
]);
$lesson->save();
}
}
答案 0 :(得分:0)
SELECT month,
year,
MAX(q_overall) -- assign the value to all rows within the same group
OVER (PARTITION BY grp)
FROM
(
SELECT all_months.month, all_months.year, p.q_overall,
-- assign a new group number whenever there's a value in q_overall
SUM(CASE WHEN q_overall IS NULL THEN 0 ELSE 1 END)
OVER (ORDER BY all_months.month, all_months.year
ROWS UNBOUNDED PRECEDING) AS grp
FROM
( -- create all months with min and max date
SELECT generate_series(date_trunc('month', min(date)),
date_trunc('month', max(date)),
interval '1 month') as date
FROM fb_parsed
) AS all_months
LEFT JOIN
( -- do the average per month calculation
SELECT EXTRACT(Month FROM date) as month,
EXTRACT(Year FROM date) as year,
ROUND(AVG(q_overall),1) as q_overall
FROM fb_parsed
WHERE business_id = 1
GROUP BY year, month
) AS p
ON EXTRACT(year FROM ym.date) = all_months.month
AND EXTRACT(month FROM ym.date) = all_months.year
) AS dt
答案 1 :(得分:0)
您的索引迭代器与您的使用不兼容。您正在获取请求参数的计数,并尝试使用它来访问每个参数的数组值。
$request->except(['_token','_method'])
可能会返回类似的内容:
[
'class_name' => ['namea', 'nameb'],
'class_desc' => ['desca', 'descb'],
'video' => ['vida', 'vidb'],
'files' => ['filesa', 'filesb'],
'free' => [true, false],
]
因此,count($lessons)
将返回(假装我的示例完全准确)5
。而您可能正在寻找2
(子阵列的大小)。
您可以像@tech建议那样做,并从其中一个子数组中获取计数以供您编制索引:
count($lessons['class_name'])
答案 2 :(得分:0)
尝试一下
public function store(Request $request)
{
$course = Course::create($request->all());
$lessons = $request->except(['_token','_method']);
for($x = 0;$x <= count($lessons); $x++)
{
$lesson = CourseClass::create([
'course_id' => $course->id,
'class_name' => $lessons[$x]['class_name'],
'class_desc' => $lessons[$x]['class_desc'],
'video' => $lessons[$x]['video'],
'files' => $lessons[$x]['files'],
'free' => $lessons[$x]['free'],
]);
$lesson->save();
}
}
我希望它会有所帮助:)