dso15221 2015-12-25 12:48
浏览 15
已采纳

选择框将ID返回为值而不是名称

Merry Christmas to you all. Hope we are all having a wonderful time? I'm sure my guess is right. Could someone help me out on this? I don't know why this code returns the id as the value when binding to a select box. All I want to do is to bind a record of MySQL DB to a select box. Though, it is smooth when not using a where clause,

This is the controller:

public function create()
    {
      $listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'id')->toArray();
        return view('product.create')->with('listCompanies', $listCompanies);
    }

This is the view:

<p>{!! Form::select('companyname', array('' => 'Select a Company') + $listCompanies) !!} </p>

When I checked the returned source page I found this:

<p><select name="companyname"><option value="" selected="selected">Select a Company</option><option value="1">New Company Nigeria</option><option value="2">Latest Company Nigeria</option></select> </p>

When I select a value from the selectbox, it returns the id, ie, 1, and that is what it adds to the DB not the actual value.

See the attached image for clarification. Please, your input is highly needed.It is suppose to return this but it doesn't

This is what it actually returns. The id of the company not the name

  • 写回答

1条回答 默认 最新

  • drllqg2903 2015-12-25 13:02
    关注

    The option value is what gets saved. If you just want the names, use lists('companyName', 'companyName')

    But typically you do want the id in your foreign key field. How do you have the relationship set up?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题