How to show selected value from database in dropdown using Laravel?

六眼飞鱼酱① 提交于 2019-12-08 11:26:42

问题


I want to show selected value from database into dropdown list on page load.

My controller index function is :

public function index()
{ 
 $country_data =DB::table('country')->select('country_id','country_name')->get();
$profile_data= DB::table('profiles')->select('*')->where('id',$user_id)->first();
return view('profile_update',compact('profile_data','country_data'));
}

Column name in database for height is :Height

My dropdown in profile_update.blade.php is

<select class="select4" name="country" id="country">
<option value="">Please Select</option>
 @foreach($country_data as $country)
<option value="{{$country->country_id}}" {{$country_data->country == $country->country_id  ? 'selected' : ''}}>{{$country->country_name}}</option>
 @endforeach</select>

回答1:


This is a example of how I do this:

<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
        <option value="option_select" disabled selected>Shoppings</option>
        @foreach($shoppings as $shopping)
            <option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id  ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
        @endforeach
    </select>



回答2:


@Sarita Sharma show the error(s). Maybe then anyone help you to resolve this problem. Show data in colections in controler - use dd e.g.

dd($country_data);
dd($profile_data);

Do you want to display the country in the view with the appropriate Height value from profile_data?

Ps. How to put the code, please use the formatting code - put the code in `` and use camelCase in value name (maintaining good PSR-1 practice) - it is easier to read code.



来源:https://stackoverflow.com/questions/50970020/how-to-show-selected-value-from-database-in-dropdown-using-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!