Search code examples
laraveldropdownhtml-select

laravel put html select dropdown "selected" at two data


I've check the data based on user details to auto selected the list from dropdown like below

<option {{ $user->grade == "10" ? 'selected' : '' }} value="10">10</option>
<option {{ $user->grade == "10 (A)" ? 'selected' : '' }} value="10 (A)">10 (A)</option>

please notice that space is there in 10 (A)

But in the form (page) both of them are putting selected (the user grade is 10)

enter image description here

how to avoid this things. Data type of grade is string

Thanks in advance


Solution

  • It seems $user->grade in not a string variable. You can cast your variable to string and then compare it

    <option {{ (string)$user->grade == "10" ? 'selected' : '' }} value="10">10</option>
    <option {{ (string)$user->grade == "10 (A)" ? 'selected' : '' }} value="10 (A)">10 (A)</option>
    

    PHP -> Type Casting