将记录移动到另一个 table 然后删除它 Laravel 8

Move record to another table then delete it Laravel 8

嗨,我有一个问题,我想把它移到另一个 table,然后从第一个 table 中删除它。 它移动记录而不是特定记录,它从列表中连续移动数据。此外,当单击存档按钮时,它会显示此错误,但仍在本地主机上工作(不显示列表):

错误异常 紧凑型():未定义的变量:archiveStudentData

控制器:

public function archiveData(){
    $archiveStudentData = User::where('role', ['athlete'])
        ->each(function ($oldPost) {
            $newPost = $oldPost->replicate();
            $newPost->setTable('archives');
            $newPost->save();
            $oldPost->delete();
            return view('users.admin.pages.archive-student-list', compact('archiveStudentData'));

        });

路线:

    Route::get('archive-student-list', [AdminController::class, 'archiveData'])->name('archive-student-list');

我认为你应该把你的 Return 像这样:

public function archiveData(){
    $archiveStudentData = User::where('role', ['athlete'])
        ->each(function ($oldPost) {
            $newPost = $oldPost->replicate();
            $newPost->setTable('archives');
            $newPost->save();
            $oldPost->delete();
        });
    
    return view('users.admin.pages.archive-student-list', compact('archiveStudentData'));
}