Select box returns ID as value not the name

- 1 answer


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




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?