1. Go to this page and download the library: Download litepie/repository 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/ */
litepie / repository example snippets
namespace App\Http\Controllers;
use App\Repositories\Contracts\UserRepositoryInterface;
class UserController extends Controller
{
public function __construct(
private UserRepositoryInterface $userRepository
) {}
public function index()
{
$users = $this->userRepository->paginate(15);
return view('users.index', compact('users'));
}
public function store(Request $request)
{
$user = $this->userRepository->create($request->validated());
return redirect()->route('users.show', $user);
}
}
namespace App\Repositories;
use App\Models\User;
use App\Repositories\Contracts\UserRepositoryInterface;
use Litepie\Repository\BaseRepository;
class UserRepository extends BaseRepository implements UserRepositoryInterface
{
public function model(): string
{
return User::class;
}
public function findActiveUsers()
{
return $this->where('status', 'active')->get();
}
public function findByEmail(string $email)
{
return $this->where('email', $email)->first();
}
public function getRecentUsers(int $days = 30)
{
return $this->where('created_at', '>=', now()->subDays($days))
->orderBy('created_at', 'desc')
->get();
}
}
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\UserRepository;
use App\Repositories\Contracts\UserRepositoryInterface;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}
}
// Much faster than OFFSET pagination for large datasets
$users = $userRepository
->where('status', 'active')
->cursorPaginate(20);
// For APIs
return response()->json([
'data' => $users->items(),
'next_cursor' => $users->nextCursor()?->encode(),
'has_more' => $users->hasMorePages(),
]);
// Automatically chooses the best pagination method based on dataset size
$users = $userRepository
->where('status', 'active')
->smartPaginate(20);
// Process large datasets without memory issues
$userRepository->chunk(1000, function ($users) {
foreach ($users as $user) {
// Process each user
}
});
// Or use lazy collections
$users = $userRepository->lazy(1000);
foreach ($users as $user) {
// Memory-efficient iteration
}