LoginSignup
1
3

More than 1 year has passed since last update.

【Laravel】セレクトボックスの実装と値の保持

Posted at

はじめに

以下のようなセレクトボックス(値は20個ぐらい格納)を実装したので、備忘録として残しておきます。
スクリーンショット 2021-10-30 21.25.01.png

コード

create.blade.php
<div class="form-group">
    <label for="category-id">{{ __('カテゴリー') }}
    <select class="form-select" id="category-id" name="category_id">
        @foreach ($categories as $category)
            <option value="{{ $category->id }}" @if(old('category_id') == $category->id) selected @endif>{{ $category->category_name }}</option>
        @endforeach
    </select>
    </label>
</div>

値を保持するために
@if(old('category_id') == $category->id) selected @endif
と記述しています。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3