2017-10-19 35 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);