Php 使用eloquent从数组列表更新多个id
我已经向控制器传递了一个id数组,它被收集在student变量中。我想为数组中的每个id更新数据库列“touch\u id\u FK”。我不知道如何使用数组id来查找学生。拉雷维尔的新城市 控制器Php 使用eloquent从数组列表更新多个id,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我已经向控制器传递了一个id数组,它被收集在student变量中。我想为数组中的每个id更新数据库列“touch\u id\u FK”。我不知道如何使用数组id来查找学生。拉雷维尔的新城市 控制器 public function setLecture($lecture,$student) { $students = student::whereIn('student_id', $student)->get(); $students->lecture_id_FK
public function setLecture($lecture,$student)
{
$students = student::whereIn('student_id', $student)->get();
$students->lecture_id_FK = $lecture;
$students->save();
//if i type "return $student" will produce -> ai160064,ai160065
}
方法,其中
方法采用。您可以使用该函数获取所有学生。在获得所有要更新的记录之后,可以使用laravel中的update方法对所有记录进行更新。这样,您可能会得到如下代码:
public function setLecture($lecture,$student)
{
$studentIds = explode(',', $student);
return student::whereIn('student_id', $studentIds)
->update(['lecture_id_FK' => $lecture]);
}
没问题。如果这个答案解决了你的问题,请随意接受。回答得好@Bryan注意到,当执行
->update([…])
操作时,它将返回一个布尔值,而不是更新的学生。@HCK我认为update实际上将返回在这种情况下受影响的行数,而不是bool。尽管如此,还是很好。@bryan很有趣。大规模更新时,我不知道这种行为。这是有道理的(在这种情况下更有用)。很好的观察。