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子句需要字符串参数不是一个 阵列,还请确保您的$可填写的字段设置的雄辩。这应该是 的窍门。