【问题标题】:How to update multiple row with different ID in Laravel如何在 Laravel 中更新具有不同 ID 的多行
【发布时间】:2020-05-01 16:19:02
【问题描述】:

这是我问的最后一个问题..

我有一张会员表

Schema::create('role_memberships', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id');
        $table->string('MembershipName');
        $table->text('MembershipValue');
        $table->timestamps();
    });

我有两行具有相同 role_id 的数据,我想更新两行 这是我的控制器

$updateArray = [['MembershipValue' => $request->PostAdMaxImage], 
['MembershipValue' => $request->PostAdExpiredDay]];
DB::table('role_memberships')->where('role_id', $id)-
>whereIn('role_id', $role->pluck('id'))->update($updateArray);

当我尝试更新时,我得到一个错误数组到字符串转换..

【问题讨论】:

  • 你能发布你的错误吗?并且 $role->pluck('id') 返回数组,因为 whereIn 将数组作为 2 个参数。 ex (->whereIn('id', [1, 2, 3]))
  • @LorenzoBerti 我确实使用 ajax 进行了更新。所以,错误只显示在检查元素中
  • foreach($updateArray as $array){ DB::table('role_memberships')->where('role_id', $id)->whereIn('role_id', $role->pluck('id'))->update($array); }
  • @SamuelJames 我使用了你给的方式,但问题是我的数据具有相同的值。例如:id = 1, role_id = 1, I do $request->PostAdMaxImage = 'hello'id = 2, role_id = 1, I do $request->PostAdExpiredDay = 'world'。然后,我保存并再次重新打开..我的 PostAdMaxImage 和 PostAdExpiredDay 的值变成了“世界”,换句话说,它只取最后一个值
  • @abr 哦,我明白了.. 谢谢您的反馈;)

标签: php laravel


【解决方案1】:

在 LARAVEL 中批量更新(批量)

https://github.com/mavinoo/updateBatch

$table = 'users';

$value = [
      [
          'id' => 1,
          'status' => 'active'
      ],
      [
          'id' => 5,
          'status' => 'deactive',
          'nickname' => 'Ghanbari'
     ],
     [
          'id' => 10,
          'status' => 'active',
         'date' => Carbon::now()
     ],
     [
         'id' => 11,
         'username' => 'mavinoo'
     ]
];

$index = 'id';

UpdateBatch::updateBatch($table, $value, $index);

【讨论】:

    【解决方案2】:

    它对我有用,即使线程是 2 年前我也想分享我的解决方案,我希望这可以帮助某人。

    在刀片中

    <input name="quantity[]">
    <input name="total[]">
    <input name="prodId[]"> 
    

    控制器

    foreach ($request->prodId as $key => $value) {
          $data = array(                 
              'quantity'=>$request->quantity[$key],
              'total'=>$request->total[$key],                   
          );         
          Cart::where('id',$request->prodId[$key])
          ->update($data); 
    }
    

    【讨论】:

      【解决方案3】:

      我认为这只是我可以使用的方式..

      DB::table('role_memberships')->where('role_id', $id)->where('MembershipName','PostAdMaxImage')->update(['MembershipValue' => $request->PostAdMaxImage]);
      DB::table('role_memberships')->where('role_id', $id)->where('MembershipName','PostAdExpiredDay')->update(['MembershipValue' => $request->PostAdExpiredDay]);
      

      如果你们有最好或最短的方法,请告诉我..谢谢;)

      【讨论】:

        【解决方案4】:

        有2个错误:

        1) whereIn 函数需要一个数组作为参数

        $users = DB::table('users')
                        ->whereIn('id', [1, 2, 3])
                        ->get();
        

        2) 更新函数需要一个简单的数组作为参数,如

        DB::table('users')
                    ->where('id', 1)
                    ->update(['votes' => 1]);
        

        你传递了一个[[], []]

        所以你必须通过示例$model-&gt;update(['MembershipValue' =&gt; $request-&gt;PostAdMaxImage])

        在这里您可以找到 updatewhereIn https://laravel.com/docs/5.5/queries 的完整文档

        【讨论】:

        • 当我返回 DB::table('role_memberships')-&gt;where('role_id', $id)-&gt;whereIn('role_id', $role-&gt;pluck('id'))-&gt;get(); 时,它返回 id = 1 和 id = 2 的会员数据
        【解决方案5】:

        试试这个。它对我有用。

          DB::table('role_memberships')->where('role_id', $id)-
        >update(['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay]);
        

        【讨论】:

        • 这对我不起作用..我已经尝试过这样的方式..结果仍然相同,它采用最后一条记录..但我不明白,检查元素中的响应像这样显示[{"id":2,"RoleName":"zxdas","IsAllCategory":false,"IsUserCanLogin":false,"created_at":"2017-10-19 14:55:46","updated_at":"2017-10-19 14:55:46"},[{"role_id":"2","MembershipName":"PostAdMaxImage","MembershipValue":"hello"},{"role_id":"2","MembershipName":"PostAdExpiredDay","MembershipValue":"world"}]]
        • 我觉得你检查角色id有问题,你检查这个变量“$id”吗?尝试在查询检查中删除“whereIn('role_id', $role->pluck('id'))”。
        【解决方案6】:

        我有两行具有相同 role_id 的数据,我想更新两行这是我的控制器

        $updateArray = [['MembershipValue' => $request->PostAdMaxImage], MembershipValue' => $request->PostAdExpiredDay]];
        DB::table('role_memberships')->where('role_id', $id)->whereIn('role_id', $role->pluck('id'))->update($updateArray);
        

        当我尝试更新时,我得到一个错误数组到字符串转换..

        那是因为您的 $updateArray 上的数组中有一个数组。像这样修复它应该可以纠正你的错误:

        $updateArray = ['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-01-24
          • 2021-10-09
          • 2021-11-29
          • 2017-02-14
          • 2019-12-02
          • 1970-01-01
          • 2011-05-01
          相关资源
          最近更新 更多