Laravel 雄辩的更新功能不是更新所有记录

Laravel 雄辩的更新功能不是更新所有记录,laravel,laravel-5,eloquent,laravel-eloquent,laravel-query-builder,Laravel,Laravel 5,Eloquent,Laravel Eloquent,Laravel Query Builder,我希望根据用户id更新所有用户记录,该用户id重复多次,我希望更新功能更新所有这些事件。这是我的尝试: App\Models\HistoryCostEstimation::where(['user_id'=>$user_ID])->update(['currency'=>$currenc]); 但此代码只更新一次,是否有其他方法可以达到所需的目的 这是表结构 这是表中数据的一个样本 我发现了错误所在,问题是我从web服务获取查询输入 想象一下移动应用程序向我发送id的场景,

我希望根据用户id更新所有用户记录,该用户id重复多次,我希望更新功能更新所有这些事件。这是我的尝试:

App\Models\HistoryCostEstimation::where(['user_id'=>$user_ID])->update(['currency'=>$currenc]);
但此代码只更新一次,是否有其他方法可以达到所需的目的

这是表结构

这是表中数据的一个样本


我发现了错误所在,问题是我从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,因为如果它不是,它将始终为null。我发现了错误所在,问题是我从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,因为如果它不是,它将始终提供null解决方案:

$user_id = 1;
$records = App\HistoryCostEstimation::where('user_id', $user_id)
                                        ->update(['cost_name' => 'New value']);
注意:雄辩的where子句采用字符串参数,而不是 数组,同时确保设置了$fillable字段。这样就可以了 诀窍


解决方案:

$user_id = 1;
$records = App\HistoryCostEstimation::where('user_id', $user_id)
                                        ->update(['cost_name' => 'New value']);
注意:雄辩的where子句采用字符串参数,而不是 数组,同时确保设置了$fillable字段。这样就可以了 诀窍


我认为应该为所有用户设置循环。这不是重点,更新函数必须更新给定输入的任何事件。在我的例子中,假设用户id=1重复10次,那么更新函数必须更新10次。我错了吗?那么,如果您没有
foreach
循环或
fluent
,那又有什么意义呢?当您更新10条具有相同id的记录时,您必须有一个循环吗。。不,它只是更新所有这些值。您可以使用
get()
获取该用户id的所有数据,然后使用循环。或者你可以尝试在eloquent中进行
sync
,我认为你应该为所有用户提供循环。这不是重点,更新功能必须更新给定输入的任何事件。在我的例子中,假设用户id=1重复10次,那么更新函数必须更新10次。我错了吗?那么,如果您没有
foreach
循环或
fluent
,那又有什么意义呢?当您更新10条具有相同id的记录时,您必须有一个循环吗。。不,它只是更新所有这些值。您可以使用
get()
获取该用户id的所有数据,然后使用循环。或者你也可以试着以雄辩的方式进行
sync