PHP code example of mattsplat / dynamic-loading

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

    

mattsplat / dynamic-loading example snippets


Collection::dynamicLoad(
    string   $relation_name,
    Closure  $subQuery,
    ?string  $relation_key = null,
    ?string  $model_key    = null,
    bool     $single       = false,
    ?int     $chunkSize    = 100
): \Illuminate\Database\Eloquent\Collection

Collection::dynamicLoadWhereIn(
    string   $relation_name,
    Closure  $subQuery,     // fn (Illuminate\Support\Collection $keys) => Builder
    string   $foreign_key,  // column on the related table + the grouping key
    ?string  $model_key = null,
    bool     $single    = false
): \Illuminate\Database\Eloquent\Collection

// one query: select * from "logins" where "user_id" in (1, 2, 3, ...) and "successful" = 1
$users = User::all()->dynamicLoadWhereIn(
    'successful_logins',
    fn ($keys) => Login::where('successful', true),
    'user_id'
);

$users->dynamicLoad(
    'r_ranks',
    fn ($m) => Rank::where('name', 'like', 'r%')->where('id', '!=', $m->rank_id)
);

$users = User::all();

$users = $users->dynamicLoad(
    'r_ranks',
    fn ($m) => Rank::where('name', 'like', 'r%')->where('id', '!=', $m->rank_id)
);

$users->first()->r_ranks;   // Eloquent\Collection of Rank models

$users[0] => [
    'id'      => 1,
    'rank_id' => 5,
    'r_ranks' => [
        ['id' => 6, 'name' => 'Ranger'],
        ['id' => 8, 'name' => 'Racer'],
    ],
]

$users = User::all()->dynamicLoad(
    'latest_login',
    fn ($m) => Login::where('user_id', $m->id)->latest()->limit(1),
    null,   // $relation_key — let it default to user_id
    null,   // $model_key    — let it default to the primary key
    true    // $single
);

$users->first()->latest_login;          // a single Login model, or null
$users->first()->latest_login?->created_at;

$users[0] => [
    'id'           => 1,
    'latest_login' => ['id' => 6,  'created_at' => '2020-01-20 10:10:00'],
]
$users[1] => [
    'id'           => 2,
    'latest_login' => ['id' => 78, 'created_at' => '2020-01-25 15:10:00'],
]

$users = User::with('rank')->get()->dynamicLoad(
    'next_rank',
    fn ($m) => Rank::where('level', '>', $m->rank->level)
                   ->orderBy('level')
                   ->limit(1),
    null, null,
    true
);

$users->first()->next_rank;   // the immediately higher Rank, or null

$orders = Order::all()->dynamicLoad(
    'same_day_orders',
    fn ($o) => Order::whereDate('created_at', $o->created_at->toDateString())
                    ->where('id', '!=', $o->id),
    'order_ref',      // $relation_key — the alias grouping rows back
    'reference'       // $model_key    — Order::$reference, not Order::$id
);

$users->dynamicLoad(
    'referrals',
    fn ($u) => Referral::select('referrals.*', 'referrer_id')
                       ->where('referrer_id', $u->id),
    'referrer_id',
    'id'
);

$users = User::all()
    ->dynamicLoad('latest_login', fn ($m) => Login::where('user_id', $m->id)->latest()->limit(1), null, null, true)
    ->dynamicLoad('r_ranks',      fn ($m) => Rank::where('name', 'like', 'r%')->where('id', '!=', $m->rank_id));

$users = User::all()->dynamicLoadWhereIn(
    'recent_posts',
    fn ($keys) => Post::whereNotNull('published_at')
                      ->where('published_at', '>=', now()->subMonth())
                      ->latest('published_at'),
    'author_id'          // column on posts + the key results are grouped by
);

$users->first()->recent_posts;   // Eloquent\Collection of Post models

$users = User::all()->dynamicLoadWhereIn(
    'latest_post',
    fn ($keys) => Post::latest('published_at'),
    'author_id',
    null,
    true            // $single → one Post or null per user
);