1

The element I want to pass to view:

<option value="UK">UK</option>
<option value="Mexico">Mexico</option>
//Something like this...

Part of my code:

foreach($data as $row)
{
   $output .= '<option value='."$row->Country".'>'.$row->Country.'</option>';
}

It will return:

{"table_data":"German<\/option>Mexico<\/option>Mexico<\/option>UK<\/option>Brazil<\/option>UK<\/option>","total_data":6}

So, It can only print the name between the , but can not print out the value inside the open tag.

How can I solve this?

4
  • Why not just iterate $data to your blade? Commented Dec 4, 2019 at 4:08
  • $output .= '<option value='.$row->Country.'>'.$row->Country.'</option>'; will return same result. (delete the double quotes of value property) Commented Dec 4, 2019 at 4:08
  • Oh, I should try that... thanks. If I still want to do it this way, how can I do? Commented Dec 4, 2019 at 4:10
  • why don't you pass data to blade and writing logic in blade view ? Commented Dec 4, 2019 at 4:52

2 Answers 2

1

why not you just do it like this:

in the controller

public function country(){
    $countries=Country::all();
    return view('country',compact('countries'));
}

in the blade view

<select>
  @foreach($contries as $country) 
    <option value="{{$country->name}}">{{$country->name}}</option>
  @endforeach
</select> 
Sign up to request clarification or add additional context in comments.

Comments

1

Please Check This In your Controller passing the countries

public function country(){
    $countries=Country::all();
    return view('your_blade_file',compact('countries'));
}

In your blade option value is $country->id option show it $country->name

<select>
  @foreach($contries as $country) 
    <option value="{{$country->id}}">{{$country->name}}</option>
  @endforeach
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.