PHP code example of basanta / lazyloader

1. Go to this page and download the library: Download basanta/lazyloader 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/ */

    

basanta / lazyloader example snippets


$usersWithClients = LazyLoader::make($users)->load(Client::class, 'clients')
    ->on([
        'clients.created_by' => 'id',
        'clients.assigned_to' => 'id'
    ])
    ->multi([
        'clients.email', 'clients.first_name', 'clients.last_name',
    ]);

$clientsWithUser = LazyLoader::make($clients)->load(User::class, 'user')
    ->on([
        'users.id' => [
            'created_by',
            'assigned_to'
        ]
    ])
    ->single([
        'users.id', 'users.first_name', 'users.last_name',
    ]);

$clientsWithUser = LazyLoader::make($clients)->load(User::class, 'user')
    ->on([
        'users.id' => [
            'created_by',
            'assigned_to'
        ]
    ])
    ->where('users.is_active', '=', 1)
    ->single([
        'users.id', 'users.first_name', 'users.last_name',
    ]);

$clientsWithUser = LazyLoader::make($clients)->load(User::class, 'user')
    ->on([
        'users.id' => [
            'created_by',
            'assigned_to'
        ]
    ])
    ->when(function($item) {
        // custom logic
        // lazy load user model only when missing
        return empty($item['user']);
    })
    ->single([
        'users.id', 'users.first_name', 'users.last_name',
    ]);