lip 2 часов назад
Родитель
Сommit
099c4008a7

+ 4 - 1
app/Http/Controllers/admin/RechargeChannel.php

@@ -22,9 +22,12 @@ class RechargeChannel extends Controller
 
             $query = new RechargeChannelGroup();
             $count = $query->count();
-            $list = $query->with(['rechargeChannel'])
+            $list = $query
                 ->forPage($page, $limit)
                 ->get();
+            foreach($list as &$item) {
+                $item->rechargeChannel = RechargeChannelModel::whereIn('id', $item['channel_ids'])->get();
+            }
         } catch (Exception $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
         }

+ 2 - 5
app/Http/Controllers/admin/User.php

@@ -198,13 +198,10 @@ class User extends Controller
     {
         try {
             $params = request()->validate([
-                'member_id' => ['required', 'string', 'min:1'],
+                'member_id' => ['required', 'array'],
                 'recharge_channel_group_id' => ['required', 'integer', 'min:1'],
             ]);
-            $user = UserModel::where('member_id', $params['member_id'])->first();
-            if (!$user) throw new Exception("用户不存在", HttpStatus::CUSTOM_ERROR);
-            $user->recharge_channel_group_id = $params['recharge_channel_group_id'];
-            $user->save();
+            UserModel::whereIn('member_id', $params['member_id'])->update(['recharge_channel_group_id' => $params['recharge_channel_group_id']]);
         } catch (ValidationException $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
         } catch (Exception $e) {

+ 6 - 4
app/Models/RechargeChannelGroup.php

@@ -4,18 +4,20 @@ namespace App\Models;
 
 class RechargeChannelGroup extends BaseModel
 {
-
     protected $table = 'recharge_channel_group';
     protected $fillable = ['name', 'channel_ids'];
     protected $hidden = [];
 
+    // 修改器:将逗号分隔字符串转为数组
     public function getChannelIdsAttribute($value)
     {
         return $value ? explode(',', $value) : []; 
     }
 
-    public function rechargeChannel()
+    // 设置器:将数组转为逗号分隔字符串
+    public function setChannelIdsAttribute($value)
     {
-        return $this->hasMany(RechargeChannel::class, 'channel_ids', 'id');
+        $this->attributes['channel_ids'] = is_array($value) ? implode(',', $value) : $value;
     }
-}
+
+}