skip to Main Content

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:xampphtdocslaralastresourcesviewsview.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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search