简体   繁体   English

如何从db中的json填充Laravel / Blade复选框

[英]How do I populate Laravel/Blade checkboxes from json in a db

Laravel is great that it auto populates the fields for you on an edit form. Laravel很棒,它可以在编辑表单上自动为您填充字段。 However i seem to be having bother getting this to work for checkboxes. 但是,我似乎很麻烦让此复选框适用于工作。

I have a list of disciplines that get stored as a json array in the database like so ["FULL_CONTACT","K1"] 我有一个学科列表,它们以json数组的形式存储在数据库中,例如["FULL_CONTACT","K1"]

How do I get these to display as checked in the form? 如何使它们在表单中显示为选中状态?

{!! Form::model($official, array('method' => 'put', 'route' => ['officials.update', $official->id], 'class' => 'form-horizontal')) !!}
    <div class="form-group">
        <label for="" class="col-lg-3 control-label">First Name:</label>
        <div class="col-lg-9 controls">
            {!! Form::text('first_name', null, array('class' => 'form-control', 'max-length' => '50', 'required')) !!}
        </div>
    </div>

    <label class="checkbox">
        {!! Form::checkbox('disciplines[]', null) !!} Full Contact
    </label>
    <label class="checkbox">
        {!! Form::checkbox('disciplines[]', null) !!} Low Kick
    </label>
    <label class="checkbox">
        {!! Form::checkbox('disciplines[]', null) !!} K1
    </label>
{!! Form::close() !!}
{{ Form::checkbox('disciplines[]', 'Low Kick', true) }} 

这将生成以下html

  <input checked="checked" name="disciplines[]" type="checkbox" value="Low Kick">

Form::checkbox's arguments are Form::checkbox($name, $value, $checked, $options) Form :: checkbox的参数是Form::checkbox($name, $value, $checked, $options)

You can use @foreach loop to display all the checkboxs, for example: 您可以使用@foreach循环显示所有复选框,例如:

@foreach($disciplines as $discipline)
    {!! Form::checkbox('disciplines[]', $discipline, null) !!} label
@endforeach`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM