Search code examples
phplaravellaravel-5multiple-selectlaravel-form

Can't Get Multiple Select Input Value in Laravel 5.0


I want to save a Company's phones. I use multiple select in the form to lists the phones.

I'm trying to get input values from the multiple select but dd($request->input('Phones')); always returns null.

Here's a part of my form (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() !!}

Here's my JQuery to add <option> in the Select Input :

$('#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();
});

.

.

Here's the phones relationship in the Company Model (App\Models\Setting\Organization\Company.php) :

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

The Phones Attribute in Company Model :

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

.

.

And in the Phones Model (App\Models\Setting\Organization\Phone.php) :

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

.

.

The store method in 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');

}

If I remove the dd($request->input('Phones'));, nothing will be saved to the database.

Please help, thanks before.


Solution

  • for multi select you should pass Selected values, as your per code, what my understanding is, you are appending options in ddPhone, but you are not making them to be Selected. So try this.

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