问题描述
Laravel中的 save()和 update()方法之间有什么区别.
What is the difference between save() and update() method in Laravel.
在更新查询的情况下,我已经使用过save()方法,但在少数情况下,它用作更新,而在少数情况下,它用作插入查询功能.请让我知道它们之间到底有什么区别.
I have used save() method in case of update query but in few cases it acts as update and in few case it act as insert query function. Please let me know what exactly the difference between them.
推荐答案
save():您可以将其视为与sql中的INSERT等效,它将创建一个新模型(并且插入数据库)
save() : you can look to it as the equivalent of the INSERT in sql, it will create a new model (and insert it in the database)
update():您可以将其视为sql中的UPDATE的等效项,它将创建一个新模型(并将其插入数据库中)
update() : you can look to it as the equivalent of the UPDATE in sql, it will create a new model (and insert it in the database)
代码
$flight = App\Flight::find(1);
if (empty($flight)) {// you can do this condition to check if is empty
$flight= new Flight;//then create new object
}
$flight->name = 'New Flight Name';
$flight->save(); //this will UPDATE the record with id=1
更多信息文档
这篇关于在Laravel中保存与更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!