0

I want to get the id of the selected value or item from a dropdown selectbox because I want to save it in the database. So, I tried passing the it as a value to get it with an hidden field on the page but it does work. How do I do this?

This is the form page:

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

@foreach($istCompaniesId as $company)
            @if(companyname->text === $listCompaniesId->value)
                {!! Form::hidden('company_id', $listCompaniesId->value) !!}
            @endif
        @endforeach

This is the controller:

 $listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'companyname')->toArray();
      $listCompaniesId = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'id')->toArray();
        return view('product.create')
        ->with('listCompanies', $listCompanies)
        ->with('listCompaniesId', $listCompaniesId);
halfer
  • 19,824
  • 17
  • 99
  • 186
ken4ward
  • 2,246
  • 5
  • 49
  • 89

1 Answers1

1

Since you are using Laravel 5.1, I suggest not to use the form helper. You can simply combine the option tag with blade's @foreach.

<select name="company_id">
   <option value="">Choose Company</option>
   @foreach($listCompanies as $company)
     <option value="{{$company->id}}">{{$company->name}}</option>
   @endforeach
</select>
  • 1
    Thanks, Habib. I have had something like this before but whenever I select and add a value to the database it adds the id, not the real value. – ken4ward Dec 26 '15 at 13:40
  • what do you mean with 'the real value'? Do you want to save the entire selected company (id, name, etc)? – Habib Ridho Dec 26 '15 at 23:25