mercredi 4 novembre 2020

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

I ae this column in my database named "source_income" enter image description here

Which was Imploded from my EDIT page. enter image description here

Problem is after I save the record, I see all the checkboxes are checked. I understand that the cause of the problem is that I do not have something that will check if the value of the checkbox should be equal to what I have in the database.

The checkboxes on my form are not dynamic.

<div class="col-md-6 ">
    <div class="form-group ">
        <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>    

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

I literally stopped in this part "$profileSourceIncome = explode(",", $profile->source_income);"

My question is how can I able to display the checkboxes checked if the name of the checkbox is equal to any value from $profileSourceIncome[]?

Thank you so much in advance!




Aucun commentaire:

Enregistrer un commentaire