英文:
move data from a table to another with Laravel 8
问题
什么是将数据从一个表(MySql)移动到另一个表的最“正确”方式,使用Laravel(8)?我发现有些人使用replicate()
,但我不知道这是否是最佳选择。是否有一些最佳实践可供参考?
另外,如果我只想将表1中的某些列移动到表2,方法是否相同?
谢谢
英文:
What's the most 'correct' way to move data from a table (MySql) to another table with Laravel (8)? I've found some people using replicate()
but I don't know if that's the best choice. Is there some, or any, best pratice for that?
Also, if I just want to move some columns from table1 to table2 the approach would be the same?
thx
答案1
得分: -1
如果您只需要移动少量行,您可以使用 replicate()
,但如果需要移动多行,则建议Karl Hill提供的选项更好:
// 在这里,您可以创建一个复杂的查询来筛选要移动的数据
$dataToCopy = DB::table('source_table')->get()->toArray();
DB::table('destination_table')->insert($dataToCopy);
英文:
If you only need to move a few rows you can use replicate()
but if you need to move several rows, then the option suggested by Karl Hill will be better:
// here you can create a complex query to filter the data you want to move
$dataToCopy = DB::table('source_table')->get()->toArray();
DB::table('destination_table')->insert( $dataToCopy );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论