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

时间:2021-03-22 12:49:10

标签: laravel eloquent

我对数据库布局进行了一些更改。数据库设计为具有 belongsTo 关系。现在是 belongsToMany 关系。所以一行现在可以有比以前更多的关系。以前,我在 $game 列中的每个 genre_id 行上存储了“流派 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 |
+----+------------------+-------------+------------+------------+

2 个答案:

答案 0 :(得分:1)

对于表单验证:

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

答案 1 :(得分:0)

试试这个

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

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

相关问题