如何在 AdminLte multi select 下拉列表中加载项目以进行更新 - Laravel 8

How to load items in AdminLte multi select dropdown for update - Laravel 8

我有一个 ClassController,它将 $players 和 $classPlayers 集合发送到视图。

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $classe = Classe::findOrFail($id);
    $players = User::whereRoleIs(['player'])->get();
    $classePlayers = User::where('classe_id', '=', $classe->id)->get();
    
    return view("admin.classes.edit", compact('classe', 'players', 'classePlayers'));
}

集合 $players 包含一对多玩家:

  #items: array:6 [▼
    0 => App\Models\User {#1338 ▼
      #fillable: array:4 [▼
        0 => "name"
        1 => "email"
        2 => "password"
        3 => "matricule"
      ]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▶]
      #original: array:10 [▶]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }

集合 $classPlayers 包含 class(一对多关系)

的玩家
Illuminate\Database\Eloquent\Collection {#1331 ▼
  #items: array:4 [▼
    0 => App\Models\User {#1324 ▶}
    1 => App\Models\User {#1335 ▼
      #fillable: array:4 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 5
        "name" => "Joueur X"
        "email" => "joueur.x@gmail.com"
        "email_verified_at" => "2021-07-28 20:04:40"
        "password" => "y$MWA6G0PjV4qm0ZVgyB3MQO0Sn2x1IN3.W9zPwIljKNwQms4eaTaEm"
        "remember_token" => "BI7R6Zw1HV"
        "created_at" => null
        "updated_at" => "2021-07-29 01:26:29"
        "matricule" => "XYZ"
        "classe_id" => 13
      ]
      #original: array:10 [▶]
      #changes: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
    2 => App\Models\User {#1346 ▶}
    3 => App\Models\User {#1347 ▶}
  ]
}

我想显示所有玩家的列表,并让class内的玩家被选中。

我试过类似的东西:

                                    <select class="selectPlayers @error('players') is-invalid @enderror" id="players"
                                        name="players[]" multiple="multiple" style="width: 100%;">
                                        @foreach($classePlayers as $player)
                                        <option value="{{$player->id}}" {{ in_array($player->id, $players->toArray()) ? "selected": ""}}>
                                            {{ $player->name }}
                                        </option>
                                        @endforeach
                                    </select>

但我得到了错误的结果(下拉列表中只有 class玩家值但未被选中)

有什么帮助吗?谢谢

您应该检查 $players 的 ID 中的 $player->id,而不是检查不包含 ID 的玩家数组。所以我将从集合中提取 $players 的 id,然后将其转换为数组 $players->pluck('id')->toArray().

{{ in_array($player->id, $players->pluck('id')->toArray()) ? "selected": ""}}

或者您可以通过

从控制器获取模型密钥 (ids)
$playerIds = User::whereRoleIs(['player'])->modelKeys();

#modelKeys()

然后只需在 select 选项循环中检查该数组。这将节省用户 RAM

 {{ in_array($player->id, $playerIds) ? "selected": ""}}