如何预先选中表单中具有belongsToMany关系的复选框



我对数据库布局做了一些更改。DB是按照belongsTo关系设计的。现在它是一个belongsToMany关系。因此,一场争吵现在可以比以前有更多的关系。之前我存储了";流派id";在CCD_ 4列中的每个CCD_。这是有效的,但它令人沮丧,因此,在我的情况下,一款游戏可以有很多类型。

在更改之前,我可以从下拉列表中选择流派,并对列表进行预检查。

<select
class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
name="genre_id"
id="genre_id">
@foreach($genres as $genre)
<option value="{{ $genre->id }}" {{ old("genre_id", $game->genre_id) == $genre->id ? 'selected' : '' }}>{{ $genre->genre }}</option>
@endforeach
</select>

通过电流设置,我在$game上获得了genres的集合。

{
"id": 12,
"title": "Mach Rider",
"slug": "mach-rider",
"console_id": 1,
"cover_image": "http://sdb3.test/storage/images/placeholder.png",
"description": null,
"deleted_at": null,
"created_at": "2017-04-21T07:40:22.000000Z",
"updated_at": "2021-03-15T10:04:24.000000Z",
"genres": [
{
"id": 36,
"genre": "Vehicular combat",
"description": null,
"created_at": "2020-12-12",
"updated_at": "2020-12-12",
"pivot": {
"game_id": 12,
"genre_id": 36
}
},
{
"id": 5,
"genre": "Racing",
"description": null,
"created_at": "2020-12-12",
"updated_at": "2020-12-12",
"pivot": {
"game_id": 12,
"genre_id": 5
}
}
]
}

与其使用下拉列表,不如使用一堆复选框。但我无法将其用于old()和预先检查的流派。我试过使用in_array()array_intersect,如下所示:

@foreach($genres as $genre)
<div class="flex items-start">
<div class="flex items-center h-5">
<input
id="genre"
name="genre[]"
type="checkbox"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
value="{{ $genre->genre }}"
{{ old("genre", in_array($genre->pluck('genre'), $game->genres->pluck('genre')->toArray())) ? 'checked' : '' }}
>
</div>
<div class="ml-3 text-sm">
<p class="text-gray-500">{{ $genre->genre }} ({{ $genre->games_count }})</p>
</div>
</div>
@endforeach

此IGameController@edit

public function edit(Game $game)
{
$this->authorize('update', $game);
$genres = Genre::withCount('games')->get();
return view('game.edit.index', compact('game', 'genres'));
}

因此,我请求您帮助的是,如何将old()belongsToMany关系一起使用?

这是我的关系:

AppGame

public function genres(){
return $this->belongsToMany(Genre::class);
}

AppGenre

public function games(){
return $this->belongsToMany(Game::class);
}

game_genre

+----+----------+---------+------------+------------+
| id | genre_id | game_id | created_at | updated_at |
+----+----------+---------+------------+------------+
|  1 |        5 |       1 | 2020-12-12 | 2020-12-12 |
|  2 |       36 |       1 | 2020-12-12 | 2020-12-12 |
+----+----------+---------+------------+------------+

genres

+----+------------------+-------------+------------+------------+
| id |      genre       | description | created_at | updated_at |
+----+------------------+-------------+------------+------------+
|  1 | Shooter          | NULL        | 2020-12-12 | 2020-12-12 |
|  2 | Platform         | NULL        | 2020-12-12 | 2020-12-12 |
|  3 | Beam em up       | NULL        | 2020-12-12 | 2020-12-12 |
|  4 | Puzzle           | NULL        | 2020-12-12 | 2020-12-12 |
|  5 | Racing           | NULL        | 2020-12-12 | 2020-12-12 |
|  6 | Simulation       | NULL        | 2020-12-12 | 2020-12-12 |
| 36 | Vehicular combat | NULL        | 2020-12-12 | 2020-12-12 |
+----+------------------+-------------+------------+------------+

对于表单验证:

{{ in_array($genre->id, old('genre', $game->genres->pluck('id')->toArray())) ? 'checked' : '' }}

试试这个

{{ old("genre", in_array($genre->id, $game->genres->pluck('id')->toArray())) ? 'checked' : '' }}

如果您的表单未通过验证,仍然不是最佳解决方案。

最新更新