Jovs
Jovs

Reputation: 892

how to dynamically option the selected in html just using php?

I'm trying to dynamically the select option selected just using php in laravel but I'm getting this error:

Use of undefined constant selected - assumed 'selected' (this will throw an Error in a future version of PHP) (View: C:\xampp\htdocs\laralast\resources\views\view.blade.php)

below is my view blade

<select class="form-control" name="assign_to" id="assign_to">
    <option selected disabled>Select support</option>
    @foreach($supports as $support)
    <option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? selected : '')}}>{{$support->fullname}}</option>
    @endforeach
</select>

Can you help me with this.

Upvotes: 0

Views: 67

Answers (2)

albus_severus
albus_severus

Reputation: 3712

Please Check This use if & else

<select class="form-control" name="assign_to" id="assign_to">
    <option selected disabled>Select support</option>
    @foreach($supports as $support)
    @if($ticket->assign_by == $support->id)
    <option value="{{$support->id}}" selected>{{$support->fullname}}
    </option>
    @else
    <option value="{{$support->id}}">{{$support->fullname}}</option>
    @endforeach
</select>

Upvotes: 1

C&#224; ph&#234; đen
C&#224; ph&#234; đen

Reputation: 1951

You should quote your string (I mean you should quote the selected):

 <option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? 'selected' : '')}}>{{$support->fullname}}</option>

Your laravel is understanding that you will print the value of selected constant (since no dollar-sign $, no string quotation '' "") when the condition is true.

Upvotes: 1

Related Questions