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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色