Select box returns ID as value not the name
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.
Answer
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?
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?