Php 如何在Laravel中更新具有不同ID的多行

Php 如何在Laravel中更新具有不同ID的多行,php,laravel,Php,Laravel,这是我问的最后一个问题 我有一张会员表 Schema::create('role_memberships', function (Blueprint $table) { $table->increments('id'); $table->integer('role_id'); $table->string('MembershipName'); $table->text('MembershipValue');

这是我问的最后一个问题

我有一张会员表

Schema::create('role_memberships', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id');
        $table->string('MembershipName');
        $table->text('MembershipValue');
        $table->timestamps();
    });
我有两行具有相同角色id的数据,我想更新两行 这是我的控制器

$updateArray = [['MembershipValue' => $request->PostAdMaxImage], 
['MembershipValue' => $request->PostAdExpiredDay]];
DB::table('role_memberships')->where('role_id', $id)-
>whereIn('role_id', $role->pluck('id'))->update($updateArray);
当我尝试更新时,我得到一个错误数组到字符串的转换

有两个错误:

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

$users = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();
2) update函数需要一个简单的参数数组,如

DB::table('users')
            ->where('id', 1)
            ->update(['votes' => 1]);
您正在传递一个
[],[]]

因此,您必须通过示例
$model->update(['MembershipValue'=>$request->PostAdMaxImage])


在这里,您可以找到有关
更新
的完整文档,其中

请尝试此文档。这对我有用

  DB::table('role_memberships')->where('role_id', $id)-
>update(['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay]);
我有两行具有相同角色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];

我想这是我唯一能用的方法

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]);
如果你们有最好的路或者最短的路,请告诉我。。谢谢;)

更新LARAVEL中的批次(批量)


它为我工作,即使线程是2年前我也想分享我的解决方案,我希望这可以帮助别人

刀片式

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

你能公布你的错误吗?和$role->pluck('id')返回数组,因为其中wnat将数组作为2个参数。例如(->其中('id',[1,2,3])@LorenzoBerti我使用ajax进行更新。。因此,错误只显示在inspect元素
foreach($updateArray as$array){DB::table('role_memberships')->where('role_id',$id)->where('role_id',$role->pull('id'))->update($array)}
@SamuelJames我使用了与您相同的方法,但问题是我的数据具有相同的值。。示例:
id=1,role\u id=1,I do$request->PostAdMaxImage='hello'
id=2,role\u id=1,I do$request->postadexpireday='world'
。然后,我保存并再次打开。。我的PostAdMaxImage和PostAdExpiredDay的价值变成了“世界”,换句话说,它只需要最后一个value@abr哦,我明白了。。感谢您的反馈;)这对我不起作用。。我试过这样的方法。。结果还是一样,最后一条记录。。但我不明白,inspect元素中的响应显示如下:
[{“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”?尝试在查询检查中再次删除“where('role\u id',$role->pull('id')”)。当我返回
DB::table('role\u memberships')->where('role\u id',$id)->where('role\u id',$role->pull('id')->get()时返回id=1和id=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); 
}