0

我想从下拉选择框中获取所选值或项目的 id,因为我想将其保存在数据库中。所以,我尝试将它作为一个值传递,以便在页面上使用隐藏字段来获取它,但它确实有效。我该怎么做呢?

这是表单页面:

<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

这是控制器:

 $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);
4

1 回答 1

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>
于 2015-12-26T12:00:28.917 回答