如何在keyBy中使用paginate

I'm new to Laravel and having trouble paginating when using keyBy().

The following works:

$customers = Customers::paginate(10)

But the following gives an error: Call to undefined method Illuminate\Database\Query\Builder::keyBy()

$customers = Customers::keyBy('id')->paginate(10)

So how do I paginate when using keyBy?

I've tried inserting this into the boot method of AppServiceProvider.php with no effect:

if (!Collection::hasMacro('paginate')) {
 Collection::macro('paginate', 
 function ($perPage = 15, $page = null, $options = []) {
 $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
 return (new LengthAwarePaginator(
 $this->forPage($page, $perPage), $this->count(), $perPage, $page,$options))->withPath('');
 });
}