Laravel:创建或更新相关模型? [英] Laravel: Create or update related model?
问题描述
请对我温柔-我是Laravel菜鸟.
Please be gentle with me - I'm a Laravel noob.
因此,目前,我遍历大量用户,以确定是否需要更新相关模型(UserLocation).
So currently, I loop through a load of users deciding whether I need to update a related model (UserLocation).
如果需要创建UserLocation,我已经尽其所能,经过一番摸索之后,我提出了以下建议;
I've got as far as creating a UserLocation if it needs creating, and after a bit of fumbling, I've come up with the following;
$coords = $json->features[0]->geometry->coordinates;
$location = new UserLocation(['lat'=>$coords[1],'lng'=>$coords[0]]);
$user->location()->save($location);
我的问题是第二次,该位置可能需要更新,并且该用户已经存在一行.
My issue is that one the second time around, the Location may want updating and a row will already exist for that user.
这是自动处理的吗,还是我需要做一些不同的事情?
Is this handled automatically, or do I need to do something different?
代码读起来就像是在创建新行一样,所以不能处理需要更新的情况吗?
The code reads like it's creating a new row, so wouldn't handle the case of needing to update it?
更新-解决方案:
感谢马修,我想出了以下解决方案;
Thanks to Matthew, I've come up with the following solution;
$location = UserLocation::firstOrNew(['user_id'=>$user->id]);
$location->user_id = $user->id;
$location->lat = $coords[1];
$location->lng = $coords[0];
$location->save();
推荐答案
您应参考 Laravel API文档.我认为他们没有在常规文档"中提及这些方法,因此我理解为什么您可能没有看到它.
You should reference the Laravel API Docs. I don't think they mention these methods in the "regular docs" though so I understand why you may have not seen it.
您可以使用模型firstOrNew
或firstOrCreate
的方法.
You can use the models firstOrNew
or firstOrCreate
methods.
firstOrNew::获取匹配属性的第一条记录或实例化 它.
firstOrNew: Get the first record matching the attributes or instantiate it.
firstOrCreate::获取匹配属性的第一条记录或创建它.
firstOrCreate: Get the first record matching the attributes or create it.
例如:
$model = SomeModel::firstOrNew(['model_id' => 4]);
在上面的示例中,如果未找到model_id为4的模型,则它将创建SomeModel
的新实例.然后可以进行操作,以后再按->save()
.如果找到,则将其返回.
In the above example, if a model with a model_id of 4 isn't found then it creates a new instance of SomeModel
. Which you can then manipulate and later ->save()
. If it is found, it is returned.
您还可以使用firstOrCreate
,它不会创建新的Model实例,而是立即将新模型插入表中.
You can also use firstOrCreate
, which instead of creating a new Model instance would insert the new model into the table immediately.
所以在您的情况下:
$location = UserLocation::firstOrNew(['lat'=>$coords[1],'lng'=>$coords[0]]);
$ location将包含数据库中的现有模型,或者包含属性分别为lat
和lng
分别设置为$coords[1]
和$coords[0]
的新实例,然后可以保存或设置更多属性值需要.
$location will either contain the existing model from the DB or a new instance with the attributes lat
and lng
set to $coords[1]
and $coords[0]
respectively, which you can then save or set more attribute values if needed.
另一个例子:
$location = UserLocation::firstOrCreate(['lat'=>$coords[1],'lng'=>$coords[0]]);
$ location将包含数据库中的现有模型或具有已设置属性的新模型,除非这次没有找到该模型.
$location will either contain the existing model from the DB or a new model with the attributes set again, except this time the model will have already been written to the table if not found.
这篇关于Laravel:创建或更新相关模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!