无法在 Laravel 5.0 中获取多个 Select 输入值

Can't Get Multiple Select Input Value in Laravel 5.0

我想保存公司的手机。我在表格中使用多个 select 来列出电话。

我正在尝试从多个 select 获取输入值,但 dd($request->input('Phones')); 总是 returns null.

这是我的部分表格 (add_company.blade.php) :

{!! Form::model($company = new App\Models\Setting\Organization\Company, ['method' => 'POST', 'action' => 'Setting\Organization\CompaniesController@store', 'files'=>true]) !!}

<div class="form-group">
    <!-- Form::label parameters : LabelFor, Text -->
    {!! Form::label('NewPhone', 'Telephones : ', ['class' => 'col-lg-3 col-md-3 col-sm-3 col-xs-3']) !!}

    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
        {!! Form::text('NewPhone', null, ['class' => 'form-control', 'id' => 'txtNewPhone']) !!}
    </div>

    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
        {!! Form::button('', ['class' => 'form-control btn btn-info fa fa-plus', 'id' => 'btnAddPhone']) !!}
    </div>

</div>

<div class="form-group">
    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"></div>

    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
        {!! Form::select('Phones[]', $phones, null, ['class' => 'form-control', 'multiple' => true, 'id' => 'ddPhone']) !!}
    </div>

    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
        {!! Form::button('', ['class' => 'form-control btn btn-danger fa fa-minus', 'id' => 'btnDelPhone']) !!}
    </div>

</div>

<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
    {!! Form::submit('Add Company', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}

这是我的 JQuery 在 Select 输入中添加 <option> :

$('#btnAddPhone').on('click', function(){
    var newPhone = $('#txtNewPhone').val();
    if($("#ddPhone option[value='" + newPhone + "']").length == 0){
        $('#ddPhone').append($('<option>', {
            value: newPhone,
            text: newPhone
        }));
    }
});

$('#btnDelPhone').on('click', function(){
    var selected = $("#ddPhone option:selected").text();
    $("#ddPhone option[value='" + selected + "']").remove();
});

.

.

这是公司模型 (App\Models\Setting\Organization\Company.php) 中的电话关系:

public function phones(){
    return $this->hasMany('App\Models\Setting\Organization\Phone', 'OwnerID', 'CompanyCode');
}

公司模型中的电话属性:

public function getPhonesAttribute(){
    return $this->phones()->lists('PhoneNumber');
}

.

.

在手机模型中 (App\Models\Setting\Organization\Phone.php) :

protected $fillable = ['OwnerID', 'PhoneNumber'];
public function company(){
    return $this->belongsTo('App\Models\Setting\Organization\CompanyCode', 'CompanyCode');
}

.

.

Setting\Organization\CompaniesController中的存储方法:

public function store(CompanyRequest $request){
    $phones = $request->input('Phones');
    dd($request->input('Phones'));
    if(count($phones) > 0){
        foreach($phones as $phone){
            Phone::create(['OwnerID' => $request->CompanyCode, 'PhoneNumber' => $phone]);
        }
    }
    flash()->success('Company ' . $request->Name . ' Added.');
    return redirect('company');

}

如果我删除 dd($request->input('Phones'));,则不会将任何内容保存到数据库中。

请帮忙,先谢谢了

对于 multi select 你应该传递 Selected 值,作为你的代码,我的理解是,你在 ddPhone 中附加选项,但你没有让它们被选中。所以试试这个。

$('#btnAddPhone').on('click', function(){
    var newPhone = $('#txtNewPhone').val();
    if($("#ddPhone option[value='" + newPhone + "']").length == 0){
        $('#ddPhone').append($('<option>', {
            value: newPhone,
            text: newPhone,
            selected: true
        }));
    }
});