【问题标题】:how to Checked if value of static checkboxes are equal on what you have in your database in Laravel如何检查静态复选框的值是否与 Laravel 数据库中的值相等
【发布时间】:2020-11-04 20:16:36
【问题描述】:

我的数据库中有这个列名为“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-&gt;source_income);

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

非常感谢您!

【问题讨论】:

    标签: laravel checkbox


    【解决方案1】:

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

    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);
    } 
    

    然后,在您的刀片文件中,您可以使用 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'])
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 2020-06-04
      • 2020-03-18
      • 2016-03-08
      • 2018-08-19
      • 2015-05-25
      • 2017-07-11
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多