英文:
Laravel eloquent and a simple database query
问题
以下是翻译好的部分:
原始代码:
$task = $request['task'];
$datetask = $request['datetask'];
$timetask = $request['timetask'];
for ($i = 0; $i < count($task); $i++) {
DB::insert('insert into tasks (task, datetask, timetask) values (?, ?, ?)',
[$task[$i], $datetask[$i], $timetask[$i]]);
};
Eloquent 代码:
for ($i = 0; $i < count($task); $i++) {
$newTask = new Task;
$newTask->task = $task[$i];
$newTask->datetask = $datetask[$i];
$newTask->timetask = $timetask[$i];
$newTask->save();
}
错误:
数组转换为字符串。
错误所在行: $newTask->save();
英文:
Laravel 9. PHP 8.0.
The task is to write an array line by line. Can you tell me why writing with eloquent in this form does not work, while writing as DB select works fine. What is my problem?
Original code
$task = $request['task'];
$datetask = $request['datetask'];
$timetask = $request['timetask'];
for ($i = 0; $i < count($task ); $i++) {
DB::insert('insert into tasks (task,datetask,timetask,)values(?,?,?)',
[$task[$i],$datetask[$i],$timetask[$i]]);
};
and Eloquent code:
for ($i = 0; $i < count($task); $i++) {
$task= new Task;
$task->task = $request['task'];
$task->datetask = $request['datetask'];
$task->timetask = $request['timetask'];
$task->save();
}
Error:
> Array to string conversion.
Line:: $task->save();
答案1
得分: 4
你遇到了数组转换为字符串的问题,因此需要使用循环来处理所有三个参数值的 [i]
。
for ($i = 0; $i < count($request['task']); $i++) {
$objTask = new Task; // 不要使用已经定义的 $task
$objTask->task = $request['task'][$i]; // 这里需要 [$i]
$objTask->datetask = $request['datetask'][$i];
$objTask->timetask = $request['timetask'][$i];
$objTask->save();
}
或者,如果你直接传递数组值,那么不需要使用循环:
// for ($i = 0; $i < count($request['task']); $i++) {
$objTask = new Task; // 不要使用已经定义的 $task
$objTask->task = $request['task']; // 直接传递请求参数
$objTask->datetask = $request['datetask'];
$objTask->timetask = $request['timetask'];
$objTask->save();
// }
英文:
You are getting array to string conversion so you need to use loop with [i]
for all 3 param values
for ($i = 0; $i < count($request['task']); $i++) {
$objTask= new Task; // don't use $task, which is already defined
$objTask->task = $request['task'][$i]; // missing [$i] here
$objTask->datetask = $request['datetask'][$i];
$objTask->timetask = $request['timetask'][$i];
$objTask->save();
}
Or if you are passing array values directly, then don't use loop
// for ($i = 0; $i < count($request['task']); $i++) {
$objTask= new Task; // don't use $task, which is already defined
$objTask->task = $request['task']; // passing request param directly
$objTask->datetask = $request['datetask'];
$objTask->timetask = $request['timetask'];
$objTask->save();
// }
答案2
得分: 2
在Eloquent中,你忘记使用循环变量$i
。
在你的原始代码中,你传递变量时使用了[$i]
,如下所示:
DB::insert('insert into tasks (task, datetask, timetask) values (?, ?, ?)',
[$task[$i], $datetask[$i], $timetask[$i]]);
你需要使用相同的变量:
for ($i = 0; $i < count($task); $i++) {
$objTask = new Task;
$objTask->task = $task[$i];
$objTask->datetask = $datetask[$i];
$objTask->timetask = $timetask[$i];
$objTask->save();
}
或者只需在你的数据中使用**[$i]
**:
$objTask->task = $request['task'][$i];
$objTask->datetask = $request['datetask'][$i];
$objTask->timetask = $request['timetask'][$i];
英文:
In Eloquent, you have missed to use the loop variable $i
.
In your original code you are passing variables with [$i]
as
DB::insert('insert into tasks (task,datetask,timetask,)values(?,?,?)',
[$task[$i],$datetask[$i],$timetask[$i]]);
You need to use same variable
for ($i = 0; $i < count($task); $i++) {
$task= new Task;
$task->task = $task[$i];
$task->datetask = $datetask[$i];
$task->timetask =$timetask[$i]];
$task->save();
}
Or just use [$i] with your data.
$objTask->task = $request['task'][$i];
$objTask->datetask = $request['datetask'][$i];
$objTask->timetask = $request['timetask'][$i];
答案3
得分: 0
如果您想在不使用循环的情况下插入多条记录,请尝试以下方法。
// 假设 $task 是一个多记录数组。
Task::insert($task);
英文:
If you want to insert multiple records without loop, try this.
//considering $task is muti record array.
Task::insert($task);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论