1

我想根據用戶ID更新所有用戶記錄,用戶ID重複多次,並且我希望更新函數更新所有這些事件,這裏是我的嘗試:Eloquent更新函數沒有更新所有記錄

App\Models\HistoryCostEstimation::where(['user_id'=>$user_ID])->update(['currency'=>$currenc]); 

但這段代碼只更新了一次,有沒有其他的方法來做所需的目的?

這是表結構 enter image description here

,這是數據的表中的

enter image description here

+0

我認爲你應該有所有用戶循環。 – Ibrahim

+0

這不是重點,更新函數必須更新給定輸入的任何出現。 在我的情況下,讓我們說用戶ID = 1重複10次,然後更新函數必須更新10個事件。 我錯了嗎? – Rami

+0

那麼,如果你沒有'foreach'或'fluent',那麼有什麼意義呢? – Ibrahim

回答

0

以及我發現,其中的錯誤是一個樣品,該問題是,我很從Web服務中獲取查詢輸入。

想象一下,移動應用程序發送給我的id並基於此id的情況下,我在問題中做了上面的查詢,但我沒有記住所有結果都是在運行時間之前,而不是在代碼之前就像

$HistoryCostEstimationID = $request->input('historyCostEstimationID'); 

    $type = $request->input('type'); 
    $newValue = $request->input('newValue'); 
    $currenc = $request->input('currency'); 

    $user_ID = App\Models\HistoryCostEstimation::select('user_id')->where('id',$HistoryCostEstimationID)->get(); 
    App\Models\HistoryCostEstimation::where('user_id', $user_ID)->update(['currency'=>$currenc]); 
    App\Models\HistoryCostEstimation::where(['id' => $HistoryCostEstimationID])->update([ $type => $newValue , 'currency' => $currenc]); 

,但它必須是這樣的

$HistoryCostEstimationID = $request->input('historyCostEstimationID'); 

    $type = $request->input('type'); 
    $uID = $request->input('u_id'); 
    $newValue = $request->input('newValue'); 
    $currenc = $request->input('currency'); 

    App\Models\HistoryCostEstimation::where('user_id', $uID)->update(['currency'=>$currenc]); 
    App\Models\HistoryCostEstimation::where(['id' => $HistoryCostEstimationID])->update([ $type => $newValue , 'currency' => $currenc]); 

我的意思是,我不得不採取U_ID執行查詢之前,因爲如果它不是,它總是給空

3

解決方案:

$user_id = 1; 
$records = App\HistoryCostEstimation::where('user_id', $user_id) 
             ->update(['cost_name' => 'New value']); 

注:where子句需要字符串參數不是一個 陣列,還請確保您的$可填寫的字段設置的雄辯。這應該是 的竅門。