Home > Blockchain >  Laravel selected, auto selected value based on user group_id
Laravel selected, auto selected value based on user group_id

Time:11-30

hello i have created a form which had an selected option value for group_id like for example :

enter image description here

but there was some user which they already had an group_id, and i was planning to make the user which they already had their own group_id only able to choose and select their own group_id, and also the select option value only shown their own group_id, here what i have been done so far :

<div class="row mg-t-20">
    <label class="col-sm-4 form-control-label">Group Sales:</label>
    <div class="col-sm-8 mg-t-10 mg-sm-t-0">
        <select name="group_id" class="form-control select2" @if(auth()->user()->group_id) readonly @endif>
            @if(auth()->user()->group_id)
                    <option value="{{auth()->user()->group_id}}">{{ $group->id}}</option>
            @elseif(!auth()->user()->group_id)
                    <option label="Choose Group..."></option>
                @foreach($groups as $group)
                    <option value="{{ $group->id }}">{{ $group->id }}</option>
                @endforeach
            @endif
        </select>
    </div>
</div>

i managed to catch user auth()->user()->group_id or group_id in hidden value to make sure the data is being catch here, so far i only managed to catch user group_id only and the option value remain the same, can someone provide a solution?, thanks!.

CodePudding user response:

So, if they're already assigned to a group, you want them to only be able to select their group in the dropdown, but if they're not already assigned to a group then you want them to be able to select any group?

In which case, it's something like this you're after (although I'm not sure why your existing code isn't getting you there, although it's a little convoluted) :

<?php $existing = Auth::user()->group_id ?>
<select name="group_id" class="form-control select2" @if($existing != "") disabled @endif>
@if($existing != "")
    <option value="{{ $existing}}" selected>{{ $existing }}</option>
@else
<option label="Choose Group..."></option>
    @foreach($groups as $group)
        <option value="{{ $group->id }}">{{ $group->id }}</option>
    @endforeach
@endif
</select>
  • Related