Matched data inside select supposed to selected by default

58 views Asked by At

I have a database that contains project_development and developer. I want to make an edit form for the project_development that has select with option from developer table. when the data matches, the current option will be selected. It works fine on other select without the foreach loop statement.

<select class="form-control" id="project" name="project">
         @foreach ($developer as $dev)
              @if ($dev->id == $project_development->developer) 
                   <option value="{{ $dev->id }}" selected>{{ $dev->name }} (Current)</option>
              @else
                   <option value="{{ $dev->id }}">{{ $dev->name }}</option>
              @endif
         @endforeach
    </select>

my other select object without looping. my other select object without looping

Thank you for your attention.

2

There are 2 answers

0
Basit On BEST ANSWER

You can do:

<option 
{!! ($dev->id == $project_development->developer) ? 'selected' ? '' !!} 
value="{{ $dev->id }}"> {{ $dev->name }}
{!! ($dev->id == $project_development->developer) ? '(Current Status)' ? '' !!}</option>
0
Nanda Pandyatama On

solved this problem..

<select class="form-control" name="request" id="request">
   <option disabled selected style='display:none;'>Choose Request...</option>
   <option value="E"@if ($projectdevelopment->request == 'E') selected>External (<i>Current Type</i>)</option> @else >External</option>@endif
   <option value="I"@if ($projectdevelopment->request == 'I') selected>Internal (<i>Current Type</i>)</option> @else >Internal</option>@endif
</select>