PHP code example of joggapp / laravel-eager-loaders

1. Go to this page and download the library: Download joggapp/laravel-eager-loaders 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/ */

    

joggapp / laravel-eager-loaders example snippets


protected array $allowedIncludes = [
    'comments',
];

protected array $allowedIncludes = [
    'flagged_comments',
];

protected array $loaderMap = [
    'flagged_comments' => 'flaggedComments',
];

protected function flaggedComments()
{
    $this->load(['comments' => function ($query) {
        $query->where('flagged', true);
    }]);
}

use App\Model\Post;
use App\EagerLoaders\PostEagerLoader;

$post = Post::first();

(new PostEagerLoader())
    ->add(['comments'])
    ->applyTo($post);

use App\Model\Post;
use App\EagerLoaders\PostEagerLoader;

$post = Post::first();

(new PostEagerLoader())
    ->add(['*'])
    ->applyTo($post);
bash
php artisan make:eager-loader PostEagerLoader