sarwan thapa
sarwan thapa

Reputation: 21

Laravel if condition is match disable otherwise don't disable

I want to display option tags in a condition like if value is equals to 1 disable option with value 1. but if its not equal don't disable

I have tried as below, but i don't think that way is possable

<select class="form-control" required>
    @foreach($timeline as $value)

        @if ($value == '1' )
            <option disabled>1</option>  <!--  disable option 1 -->
        @elseif ( $value == '2')
            <option disabled>2</option>  <!-- disable option 2 -->
        @elseif ( $value == '6')
            <option disabled>6</option>  <!-- disable option 6 -->
        @else
            <option>1</option>  
            <option>2</option>
            <option>6</option>
            <option>7</option>
        @endif

    @endforeach

    <option>8 pm </option>
    <option>9 pm </option>
</select>

Upvotes: 2

Views: 2106

Answers (2)

buffy
buffy

Reputation: 546

You could try doing it like this:

<option {{ $value == '6' ? 'disabled' : '' }}>6</option>

What this does is, if the value equals 6, it's going to echo disabled in that specific place. If it's some other value it's going to still call echo, but with an empty string which results in no output and thus does not disable anything.

Upvotes: 5

Julius Fasema
Julius Fasema

Reputation: 902

try this

<select class="form-control" id="optvalue" name="optvalue">

               @foreach($timeline as $value)

                  @if ($value->id == '1' )
                  <option value="1" {{ (old('optvalue') == 1) ? "disabled" : "" }}>1</option>  <!--  disable option 1 -->
                  @elseif ( $value->id == '2')
                  <option value="2" {{ (old('optvalue') == 2) ? "disabled" : "" }}>2</option>  <!-- disable option 2 -->
                  @elseif ( $value->id == '6')
                  <option value="6" {{ (old('optvalue') == 6) ? "disabled" : "" }}>6</option>  <!-- disable option 6 -->
                  @else
                  <option>1</option>  
                  <option>2</option>
                  <option>6</option>
                  <option>7</option>
                  @endif
              @endforeach
                  <option>8 pm </option>
                  <option>9 pm </option>
                </select>

Upvotes: 0

Related Questions