Laravel Eloquent 和一个简单的数据库查询

huangapple go评论94阅读模式
英文:

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[&#39;task&#39;];
$datetask = $request[&#39;datetask&#39;];
$timetask = $request[&#39;timetask&#39;];
        
for ($i = 0; $i &lt; count($task ); $i++) {
    DB::insert(&#39;insert into tasks (task,datetask,timetask,)values(?,?,?)&#39;,
                [$task[$i],$datetask[$i],$timetask[$i]]);
};

and Eloquent code:

for ($i = 0; $i &lt; count($task); $i++) {
    $task= new Task;
    $task-&gt;task = $request[&#39;task&#39;];
    $task-&gt;datetask = $request[&#39;datetask&#39;];
    $task-&gt;timetask = $request[&#39;timetask&#39;];
    $task-&gt;save();
}

Error:

> Array to string conversion.

Line:: $task-&gt;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 &lt; count($request[&#39;task&#39;]); $i++) {
  $objTask= new Task; // don&#39;t use $task, which is already defined
  $objTask-&gt;task = $request[&#39;task&#39;][$i]; // missing [$i] here
  $objTask-&gt;datetask = $request[&#39;datetask&#39;][$i];
  $objTask-&gt;timetask = $request[&#39;timetask&#39;][$i];
  $objTask-&gt;save();
}

Or if you are passing array values directly, then don't use loop

  // for ($i = 0; $i &lt; count($request[&#39;task&#39;]); $i++) {
  $objTask= new Task; // don&#39;t use $task, which is already defined
  $objTask-&gt;task = $request[&#39;task&#39;]; // passing request param directly
  $objTask-&gt;datetask = $request[&#39;datetask&#39;];
  $objTask-&gt;timetask = $request[&#39;timetask&#39;];
  $objTask-&gt;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(&#39;insert into tasks (task,datetask,timetask,)values(?,?,?)&#39;,
                [$task[$i],$datetask[$i],$timetask[$i]]);

You need to use same variable

for ($i = 0; $i &lt; count($task); $i++) {
    $task= new Task;
    $task-&gt;task = $task[$i];
    $task-&gt;datetask = $datetask[$i];
    $task-&gt;timetask =$timetask[$i]];
    $task-&gt;save();
}

Or just use [$i] with your data.

 $objTask-&gt;task = $request[&#39;task&#39;][$i];
 $objTask-&gt;datetask = $request[&#39;datetask&#39;][$i];
 $objTask-&gt;timetask = $request[&#39;timetask&#39;][$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);

huangapple
  • 本文由 发表于 2023年8月10日 17:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定