eli-thig
4/30/2019 - 6:24 AM

Laravel - Array to string in multiple select input field

<!--  Create -->
<!--  In Controller (store)-->
// Target Emails
        if (isset($request->target_emails)) {
            $target_emails_string = implode (", ", $request->target_emails);
        }
// store the value
$company->target_emails       = $target_emails_string;

<!--  In Blade -->
<div class="form-group col-xs-12 col-md-6">
  {{ Form::label('target_email', 'Target Email') }}
  {{ Form::select('target_emails[]', [], Input::old('target_emails[]'), [ 'class' => 'form-control, select2 select-2-skip w-100', 'id' => 'target_emails', 'multiple' => 'multiple']) }}
</div>

<!--  Edit -->
<!--  In Controller -->
$target_emails = explode(", ", $company->target_emails);
        $target_emails_array = [];
        foreach($target_emails as $email) {
            $target_emails_array[$email] = $email;
        }
        
//return view
->with('target_emails_array', $target_emails_array)

<!-- In Blade -->
<div class="form-group col-xs-12 col-md-6">
  {{ Form::label('target_emails', 'Target Email') }}
  {{ Form::select('target_emails[]',  $target_emails, array_keys($target_emails), [ 'class' => 'form-control, select2 select-2-skip w-100', 'id' => 'target_emails', 'multiple' => 'multiple']) }}
</div>


<script>
  $( document ).ready(function() {
    $('#target_emails').select2({
        tags: true,
        
  createTag: function (params) {
      console.log("here!")
    // Don't offset to create a tag if there is no @ symbol
    if (validateEmail(params.term)) {
      // Return null to disable tag creation
      return {
      id: params.term,
      text: params.term
    }} else {
        return null;
    
    }

    
  }
});
});
</script>