Laravel Select2 old input after validation
<p>I'm using Select2 in my webapplication. I load my Select2 boxes with Ajax. When validation fails, all the inputs are filled as before except the Select2 box. How can I restore the old value after the form validation fails? My bet was using <code>Request::old('x')</code>, but this inserts the value (in my case an user ID) instead of the selected text. So for example the text <code>John</code> would become <code>27</code> in the selectbox. How can I get the text back? </p>

<pre><code>&lt;select id="customer" name="customer" class="searchselect searchselectstyle"&gt;
&lt;/select&gt;
</code></pre>

<p>The js:</p>

<pre><code>token = '{{csrf_token()}}';

$(".searchselect").select2({
    ajax: {
        dataType: "json",
        type: "POST",
        data: function (params) {
            return {
                term: params.term,
                '_token': token,
                'data' : function(){
                    var result = [];
                    var i = 1;
                    $('.searchselect').each(function(){
                        result[i] = $(this).val();
                        i++;
                    });
                    return result;
                }
            };
        },
        url: function() {
            var type = $(this).attr('id');
            return '/get' + type;
        },
        cache: false,
        processResults: function (data) {
            return {
                results: data
            };
        }
    }
});
</code></pre>

<p><strong>Edit</strong></p>

<p>The only (dirty) solution I found so far is the following:</p>

<pre><code> &lt;select id="customer" name="customer" class="searchselect searchselectstyle"&gt;
    @if(Request::old('customer') != NULL)
        &lt;option value="{{Request::old('customer')}}"&gt;{{$customers-&gt;where('id', intval(Request::old('customer')))-&gt;first()-&gt;name}}&lt;/option&gt;
    @endif
&lt;/select&gt;
</code></pre>

<p><code>$customers</code> is a list of all customers, so this means that for each Select2 box I need to query a big list of items in order to make it work. This will be pretty inefficient if we're talking about thousands of rows per Select2 box.</p>

<p>I guess there must be a better solution. Who can help me?</p>