-1

I have edit form where i need to set option value as select=selected based on database value

<option selected="selected" value="2">laravel</option>

i have following code

 @foreach($category_list as $data)

  <option value="{{$data->id }}" >{{$data->cat_name}}</option>

                  @endforeach 

Now i need to compare {{$data->id }} with database value id {{$single_edit_product->id}} and set option value as selected="selected".i know i can do using ternary but i failed to do using blade template.

can anyone help me ?

Updated

@foreach($category_list as $data)

            <?php  $d1=$single_edit_product->id ;$d2=$data->id; ?>
                 <option value="{{$data->id}} " <? ($d1==$d2) ? selected='selected' ?>>{{$data->cat_name}}</option>
              @endforeach 
scott
  • 3,112
  • 19
  • 52
  • 90
  • The problem is that your ternary statement is not working... posting that would be a good start. – Jeemusu Sep 02 '15 at 05:01

2 Answers2

1

Try this

  @foreach($category_list as $data)
   @if($data->id == 'option value which need to be compared')
  <option value="{{$data->id }}" selected>{{$data->cat_name}}</option>
   @else
  <option value="{{$data->id }}">{{$data->cat_name}}</option>
   @endif   

   @endforeach 
Tal
  • 1,091
  • 12
  • 25
1

The problem with your ternary statement is that it isn't compatible with blade. Instead of using short php tags <? ?> you should be wrapping it in blade tags {{ }}.

@foreach($category_list as $data)

    <option value="{{$data->id }}" {{ $data->id == $single_edit_product->id ? 'selected="selected"' : '' }}>{{$data->cat_name}}</option>

@endforeach 
Jeemusu
  • 10,415
  • 3
  • 42
  • 64
  • @Jeemusu.thank you for the answer.i tried but it giving me wrong category name – scott Sep 02 '15 at 05:31
  • Try debugging it. Look at the source of your page, check the id of the option that is selected, is it correct? `echo` out the value of `$single_edit_product->id` and compare them. – Jeemusu Sep 02 '15 at 05:34
  • iam facing one problem.can you post answe if you know.http://stackoverflow.com/questions/32368489/how-to-authenticate-using-socialite – scott Sep 03 '15 at 06:43