如何检查 Laravel 中静态复选框的值是否与数据库中的值相等

how to Checked if value of static checkboxes are equal on what you have in your database in Laravel

我的数据库中有这个名为“source_income”的列

这是从我的编辑页面内爆的。

问题是在我保存记录后,我看到所有的复选框都被选中了。我知道问题的原因是我没有检查复选框的值是否应该等于我在数据库中的值的东西。

我表单上的复选框不是动态的。

<div class="col-md-6 ">
    <div class="form-group {{ $errors->has('source_income') ? 'has-error' : ''}}">
        <strong>Check all that apply to you:</strong>
        <br>
        <br>
        {!! Form::checkbox('source_income[]', 'employed', null, ['id' => 'employed']) !!}            
        {!! Form::label('employed', 'Employed') !!}
        <br>
        {!! Form::checkbox('source_income[]', 'online-seller', null, ['id' => 'online-seller']) !!}
        {!! Form::label('online-seller', 'Online Seller') !!}
        <br>
        {!! Form::checkbox('source_income[]', 'rider', null, ['id' => 'rider']) !!}            
        {!! Form::label('rider', 'Rider (Grab,Lazada,Etc.)') !!}                        
        <br>
        {!! Form::checkbox('source_income[]', 'small-business-owner', null, ['id' => 'small-business-owner']) !!}
        {!! Form::label('small-business-owner', 'Small Business Owner') !!}
        <br>
        {!! Form::checkbox('source_income[]', 'no-income', null, ['id' => 'no-income']) !!}            
        {!! Form::label('no-income', 'No income') !!}
        <br>
        {!! Form::checkbox('source_income[]', 'remittances-allotment', null, ['id' => 'remittances-allotment']) !!}
        {!! Form::label('remittances-allotment', 'Remittances / Allotment') !!}
        {!! $errors->first('source_income', '<p class="help-block">:message</p>') !!}
    </div>
</div>

编辑,BLADE.PHP

public function edit($id, Profile $model)
{
    $user_id = Auth::user()->id;
    $user = User::findOrFail($user_id);

    $profile = Profile::findOrFail($id);
    $profileSourceIncome = explode(",", $profile->source_income); 

    return view('dashboard.profile.edit', compact('model','profile'))
        ->with('user', $user)
        ->with('profileSourceIncome', $profileSourceIncome);
}

我真的在这部分停了下来$profileSourceIncome = explode(",", $profile->source_income);

我的问题是,如果复选框的名称等于 $profileSourceIncome[] 中的任何值,我如何才能显示选中的复选框?

在此先感谢您!

根据 documentation,您只需要在 Form::checkbox(...) 调用中将 true 作为第三个参数传递给 return 已选中的复选框。

public function edit($id, Profile $model)
{
    $user_id = Auth::user()->id;
    $user = User::findOrFail($user_id);

    $profile = Profile::findOrFail($id);
    // Turn this array into a Collection
    $profileSourceIncome = collect(explode(',', $profile->source_income)); 

    return view('dashboard.profile.edit', compact('model','profile'))
        ->with('user', $user)
        ->with('profileSourceIncome', $profileSourceIncome);
} 

然后,在您的 blade 文件中,您可以使用 Collection's contains() method 执行以下操作:

Form::checkbox('source_income[]', 'employed',              $profileSourceIncome->contains('employed'),              ['id' => 'employed'])
Form::checkbox('source_income[]', 'online-seller',         $profileSourceIncome->contains('online-seller'),         ['id' => 'online-seller']) 
Form::checkbox('source_income[]', 'rider',                 $profileSourceIncome->contains('rider'),                 ['id' => 'rider'])
Form::checkbox('source_income[]', 'small-business-owner',  $profileSourceIncome->contains('small-business-owner'),  ['id' => 'business-owner'])
Form::checkbox('source_income[]', 'no-income',             $profileSourceIncome->contains('no-income'),             ['id' => 'no-income'])
Form::checkbox('source_income[]', 'remittances-allotment', $profileSourceIncome->contains('remittances-allotment'), ['id' => 'remittances-allotment'])