我有一个问题,更新所有记录的关系模式。相反,只有一个模型正在更新所有记录。我需要更新每一份记录。
我有一个包含“零售商”的模型,这个模型与“位置”模型有一个hasMany关系。选址模型belongsTo零售商模型。
位置模型为零售商保存地址。有些零售商有许多地点(地址)。我尝试过许多解决方案,但似乎没有办法奏效。唯一有效的解决方案是使用first();更新第一条记录,但我需要更新所有的地址记录。我真的把头发扯掉了,我已经被困住了四天了。我真的准备好自杀了。任何帮助。请。
public function update(Request $request, $id)
{
$location = $request->only(
'street_number',
'street_address',
'city',
'state',
'postcode',
'country',
'longitude',
'latitude',
'country_code'
);
$insert = Location::where('id', $id);
$insert->update($location);
return Redirect::route('retailers.edit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.')
;
}发布于 2016-08-03 09:46:56
为了更新所有相关的位置记录,您应该对您的关系使用 update ()方法,如下所示:
$updatedLocationData = [
'country_code' => 123
];
$retailer->locations()->update($updatedLocationData);https://stackoverflow.com/questions/38724692
复制相似问题