更新laravel表单时如何保留select选项中的旧值?

8e2ybdfx  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(138)

当我更新表单时,如何保留选择选项中的旧值?
表格:

<select class="form-select" name="slug" id="slug" value="{{$project->slug ?? '' }}">
<option value="">Select Category </option>
<option value="commercial">commercial</option>
</select>
jslywgbw

jslywgbw1#

可以像这样使用old()

<select class="form-select" name="slug" id="slug">
    <option value="commercial" {{ old('slug', $project->slug) == 'commercial' ? 'selected' : '' }}>Commercial</option>
    <option value="another" {{ old('slug', $project->slug) == 'another' ? 'selected' : '' }}>Another</option>
    <option value="anotherrr" {{ old('slug', $project->slug) == 'anotherrr' ? 'selected' : '' }}>Anotherrr</option>
</select>
oalqel3c

oalqel3c2#

只需使用@selected(bool),如果条件为真,它将返回selected。

<select class="form-select" name="slug" id="slug">
    <option value="commercial" @selected(old('slug', $project->slug) == 'commercial')>Commercial</option>
    <option value="another" @selected(old('slug', $project->slug) == 'another')>Another</option>
</select>

相关问题