2017-10-19 36 views
0

这是最后一个问题,我问更新多行不同的ID ..如何Laravel

我有一个会员表

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); 

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

+0

你可以发布你的错误?和$ role-> pluck('id')返回数组,因为whereIn将数组作为2个参数。 ex( - > whereIn('id',[1,2,3])) – LorenzoBerti

+0

@LorenzoBerti我使用ajax更新..所以,错误只显示在检查元素 –

+0

'foreach($ updateArray as $ array){ ('role_id',$ id) - >其中('role_id',$ role-> pluck('id')) - > update($ array); }' –

回答

1

我想这是只有我能使用的方式..

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]); 

如果你们有最好的方式还是最近的路,请让我知道..三江源;)

+0

是否解决了我的问题? – abr

+0

它不工作..它取最后一个值@abr –

0

有2错误:

1),其特征在于功能希望进行参数数组作为

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

2)更新功能希望进行参数等

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

一个简单的数组您通过一个[[], []]

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

在这里你找到完整的文档updatewhereInhttps://laravel.com/docs/5.5/queries

+0

换句话说,我不能使用'whereIn'?对? –

+0

当我返回'DB :: table('role_memberships') - > where('role_id',$ id) - > whereIn('role_id',$ role-> pluck('id')) - > get() ;' 它返回id = 1和id = 2的会员数据 –

0

试试这一个。它适用于我。

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

这对我不起作用..我尝试过这样的方式..和结果仍然相同,它采取最后一个记录..但我不'不明白,在检查元素中的响应如下所示 '[{“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“}]]' –

+0

我认为你在检查角色ID时有问题,你检查这个变量” $ ID“?尝试删除查询中的“whereIn('role_id',$ role-> pluck('id'))” 检查查询。 – Kenneth

0

I have two rows data with same role_id and I want to update two rows This is my controller

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

when I tried to update, I got an error array to string conversion..

那是因为你有你的$ updateArray阵列中的数组。修复它这应该纠正你的错误:

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

UPDATE批量(BULK)的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);