是否可以在控制器上将数据存储到变量中?

时间:2018-07-05 07:03:55

标签: laravel-5.4

**trying to pass the data in database into variable and then insert to another table**  

$ query = DB :: table('students')-> where('studno',$ request-> input('stud_id'))-> get();

如何?

This is student table

and this is attendance table

我想将该行复制到另一个表中,并在其中添加时间

1 个答案:

答案 0 :(得分:0)

尝试此代码。效果很好,但是您必须指定所有剩余的列

$students = DB::table('students')->where('studno', $request->input('stud_id'))->get();
$data = [];
foreach ($students as $student) {
    $singleData['studno'] = $student->studno;
    $singleData['batch'] = $student->batch;
    ......
    // other column specify yourself
    $data[] = $singleData;
}

if ($data) {
    $isSavedAttendances = DB::table('attendances')->insert($data);
    // $isSavedAttendances must be true or false 
}