joggapp/laravel-eager-loaders 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

joggapp/laravel-eager-loaders

Composer 安装命令:

composer require joggapp/laravel-eager-loaders

包简介

Configurable eager loading for the Laravel PHP Framework

README 文档

README

This package makes it easy to load Eloquent model relationships via an associative array of options, usually from a FormRequest class.

Installation

Install the package via Composer:

composer require joggapp/laravel-eager-loaders

Usage

Defining an EagerLoader

Consider a Product model with category, name, and price fields.

We can apply sorting and filtering on these fields by defining a EagerLoader class. To create one, use the following command:

php artisan make:eager-loader PostEagerLoader

This command will create a new EagerLoader class in the App\EagerLoaders directory.

Within this class, you can define the relationships a user is allowed to load:

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

Or define a custom relationship:

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

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

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

Using EagerLoader Methods

After defining the EagerLoader, it can be used to load Eloquent model relationships:

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

$post = Post::first();

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

In this example, we are getting all comments related to the given post.

We can also load all relationshiops by following the example below:

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

$post = Post::first();

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

统计信息

  • 总下载量: 51
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固