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

问题描述 投票:0回答:2

我对数据库布局做了一些更改。 DB 的设计具有

belongsTo
关系。现在是
belongsToMany
的关系。因此,一行现在可以比以前拥有更多的关系。以前,我将“流派 ID”存储在
$game
列中的每个
genre_id
行上。这确实有效,但令人不安的是,就我而言,游戏可以有多种类型。

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

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

使用当前设置,我在

genres
上获得了
$game
的集合。

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

这个我

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

这是我的人际关系:

App\Game

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

App\Genre

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 |
+----+------------------+-------------+------------+------------+
php laravel eloquent
2个回答
1
投票

对于表单验证:

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

0
投票

试试这个

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

如果您的表单验证失败,这仍然不是最好的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.