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

Posted

技术标签:

【中文标题】如何在 AdminLte 多选下拉列表中加载项目以进行更新 - Laravel 8【英文标题】:How to load items in AdminLte multi select dropdown for update - Laravel 8 【发布时间】:2021-10-17 21:59:54 【问题描述】:

我有一个 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 包含一个类的玩家(一对多关系)

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" => "$2y$10$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 ▶
  ]

我想显示所有玩家的列表并选择 classPlayers 中的玩家。

我尝试过类似的方法:

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

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

有什么帮助吗?谢谢

【问题讨论】:

【参考方案1】:

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

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

或者您可以通过

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

#modelKeys()

然后在您的选择选项循环中检查该数组。这将节省用户 RAM

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

【讨论】:

非常感谢,我错过了从 classPlayers 中提取 id。我这样解决它: in_array($player->id, $classePlayers->pluck('id')->toArray()) ? "selected": "" 并在玩家身上迭代。 modelkeys 也是一个很好的解决方案,谢谢。

以上是关于如何在 AdminLte 多选下拉列表中加载项目以进行更新 - Laravel 8的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式在 select2 多选下拉列表中添加其他选择而不会丢失现有的选定项目

如何根据Angular 6同一行中的其他单元格值在AG-Grid选择下拉列表中加载不同的选项?

为啥在下拉列表中加载组件时不会触发 ngmodelChange 事件?

在 mvc 中加载 2 个下拉列表的更好方法

无法在angularjs中加载下拉列表的ng-model

php ajax依赖下拉列表不从表中加载数据