Help with Select2 and ajax

Hi, I’m trying to get a select2 multiselect dropdown to work with Ajax/infinite scroll in Laravel and having an issue. I have a table that has an email address and user full name. I have a getApprovers function in the controller, like this:

public function getApprovers(Request $request){

        if ($request->ajax()) {
            $term = trim($request->term);
            $approval_users = SailpointIdentity::select('email as id','full_name as text')
                ->where('full_name', 'LIKE', "%$term%")
                ->orderBy('full_name', 'asc')->simplePaginate(50);
				
            $morePages=true;
           	$pagination_obj= json_encode($approval_users);
           	if (empty($approval_users->nextPageUrl())){
            	$morePages=false;
           	}

            $results = array(
			  "results" => $approval_users->items(),
              "pagination" => array(
                "more" => $morePages
              )
            );

            return \Response::json($results);
        }
    }

Javascript:

$('#group_approvers').select2({
        ajax: {
            url: strGetApproversUrl,
            type: 'post',
            dataType: 'json',
            delay: 250,
            data: function(params) {
                return {
                    _token: $('meta[name="csrf-token"]').attr('content'),
                    q: params.term || '',
                    page: params.current_page || 1
                };
            },
            processResults: function(data, params) {
                params.current_page = params.current_page || 1;

                return {
                    results: data,
                    pagination: {
                        more: (params.current_page * 30) < data.total
                    }
                };
            },
            cache: true
        },
        dropdownParent: $("#myModal"),
        allowClear: true,
        minimuminputLength: 4,
        multiple: true
    })

The problem is the id in the response comes back as 0 instead of email address for all users, which causes the select not to work correctly.

Here is a sample of the response:

{
    "results": [
        {
            "id": 0,
            "text": "A Abdul Samad"
        },
        {
            "id": 0,
            "text": "A Spoorthi Alva"
        },
        {
            "id": 0,
            "text": "A Viswanath"
        },

Anyone have any ideas on what the problem might be and how to fix it?

Thanks,
David

Hello, I don’t think it’s a Select2 issue. I would rather see whether the server side code tries to cast the email to an integer or the email value is a null converted to 0. Did you try to define the query differently?