I've created a function that returns a json of products data, and I've wanted to display it in select2 in an infinite scroll way. Right now, I can only display according to the number of paginated results.

Here's my controller:

```
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Product;

class ReceiveInventoryController extends Controller
{
    public function index(Request $request) {
        return view('receive-inventory.index');
    }

    public function getProducts(Request $request){
        $search = $request->search;
        if($search == ''){
            $products = Product::orderby('name','asc')->select('id','name')->paginate(8);
        } else {
            $products = Product::orderby('name','asc')->select('id','name')->where('name', 'like', '%' .$search . '%')->paginate(8);
        }

        $response = array();
        foreach($products as $product){
            $response[] = array(
                "id" => $product->id,
                "text" => $product->name
            );
        }
        return response()->json($response);
    }
}

```

And here's my select2 JS ajax call:

```
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$(document).ready(function () {
    $.noConflict();
    $('#products-list').select2({
        ajax: {
            url: 'receive-inventory/get-products',
            type: 'post',
            dataType: 'json',
            delay: 250,
            data: function(params) {
                var query = {
                    _token: CSRF_TOKEN,
                    search: params.term, // search term
                    page: params.page || 1
                };

                // Query parameters will be ?search=[term]&page=[page]
                return query;
            },
            processResults: function(response) {
                return {
                    results: response
                };
            },
            cache: true
        }
    });
});

```

I think I'm already near with answering the solution. Just lack a little thing.