PHP code example of iamgerwin / nova-infinite-scroll
1. Go to this page and download the library: Download iamgerwin/nova-infinite-scroll library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iamgerwin / nova-infinite-scroll example snippets
namespace App\Nova;
use Iamgerwin\NovaInfiniteScroll\Traits\HasInfiniteScroll;
use Laravel\Nova\Resource;
class User extends Resource
{
use HasInfiniteScroll;
// Your resource definition...
}
return [
// Enable/disable infinite scroll globally (not recommended)
// Set to false to only enable for resources with HasInfiniteScroll trait
'enabled' => false,
// Number of records to load per batch
'per_page' => 25,
// Distance from bottom (in pixels) to trigger loading
'threshold' => 200,
// Customize loading messages
'loading_text' => 'Loading more records...',
'end_text' => 'All records loaded',
// Auto-enable on resource index pages with the trait
'auto_enable' => true,
// Exclude specific resources (when global enabled is true)
'excluded_resources' => [
// App\Nova\User::class,
],
];
class Article extends Resource
{
use HasInfiniteScroll;
public static function infiniteScrollPerPage(): int
{
return 50; // Load 50 articles at a time
}
public static function infiniteScrollThreshold(): int
{
return 300; // Trigger loading 300px from bottom
}
public static function infiniteScrollEnabled(): bool
{
return auth()->user()->prefersInfiniteScroll ?? true;
}
}
public static function infiniteScrollEnabled(): bool
{
// Only for admins
return auth()->user()->isAdmin();
// Or based on resource count
// return static::newModel()->count() > 100;
}
public static function infiniteScrollPerPage(): int
{
// Smaller batches on mobile
if (request()->header('User-Agent') && str_contains(request()->header('User-Agent'), 'Mobile')) {
return 15;
}
return 30;
}