2

I have table user and branch like this

id name  branch_code
1  ABC   ["011","012","013","014"]
2  DEF   ["014"]

table branch

id branch_code branch_name
1   011        XXX
2   012        YYY
3   013        ZZZ
4   014        WWW

when create a new user, there is multi-select dropdown for BRANCH, so one user can select many branches.

i can save the multi-select dropdown it into table user in column branch_code. But i have problem to show the selected value in edit form

here is the multi-select dropdown for edit form

<select class="form-control" name="branch[]" id="users" multiple>
        @foreach ($branch as $branch)
             <option @if ($branch->branch_code==$user->branch_code)
                      selected
                    @endif value="{{$branch->branch_code}}">
                    {{$branch->branch_name}}
             </option>
         @endforeach
   </select>

the edit controller

 public function edit($id)
{
  
    $branch = Branch::where('status','1')->get();
    $user = User::where('id',$id)->first();
    return view('admin.edit', compact('user','branch'));
}

in edit form, i want to show all branches with selected branches and also unselected branches in the dropdown. and for selected branches, it will highlighted with "selected" remark or something like that. But now it's only show all branches and no remark for selected branches

1 Answer 1

1

$user->branch_code is array or json string of an array.

If it's json string then $user->branch_code = json_decode($user->branch_code) first.

When you have array - you can do something like:

<option
    @if (in_array($branch->branch_code, $user->branch_code)) selected @endif
    value="{{$branch->branch_code}}"
>
    {{$branch->branch_name}}
</option>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.