2016-08-02 140 views
1

我遇到更新關係模式的所有記錄的問題。相反,只有一個模型正在更新所有記錄。我需要每條記錄進行更新。更新關係問題

我有一個擁有「零售商」的模型,該模型與「位置」模型具有hasMany關係。位置模型屬於的零售商模式。

位置模型包含零售商的地址。一些零售商有許多地點(地址)。我已經嘗試了許多解決方案,但似乎無法得到任何工作。唯一有效的解決方案是使用first();更新第一條記錄,但我需要更新所有地址記錄。我真的把我的頭髮撕了,我一直堅持4天。我真的準備好自殺了。任何幫助。請。

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.') 
    ; 
    } 
+0

您需要更新「所有」地址(位置)記錄? – lagbox

回答

0

爲了更新所有相關位置記錄,你應該在使用更新()方法對你的關係如下所示:

$updatedLocationData = [ 
    'country_code' => 123 
]; 

$retailer->locations()->update($updatedLocationData);