lorisleiva/laravel-add-select 问题修复 & 功能扩展

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

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

lorisleiva/laravel-add-select

Composer 安装命令:

composer require lorisleiva/laravel-add-select

包简介

README 文档

README

🧱 Add subSelect queries to your Laravel models using dynamic methods.

If you're not familiar with subSelect queries, I strongly recommend this article by Johnathan Reinink.

Installation

composer require lorisleiva/laravel-add-select

Usage

Consider two Eloquent models Book and Chapter such that a book can have multiple chapters.

By using the AddSubSelects trait, you can now add subSelect queries to the Book model by following the naming convention add{NewColumnName}Select. For example, the following piece of code add two new subSelect queries to the columns last_chapter_id and latest_version.

class Book extends Model
{
    use AddSubSelects;

    public function addLastChapterIdSelect()
    {
        return Chapter::select('id')
            ->whereColumn('book_id', 'books.id')
            ->latest();
    }

    public function addLatestVersionSelect()
    {
        return Chapter::select('version')
            ->whereColumn('book_id', 'books.id')
            ->orderByDesc('version');
    }
}

Now, you can eager-load these subSelect queries using the withSelect method.

Book::withSelect('last_chapter_id', 'latest_version')->get();

You can also eager-load models that are already in memory using the loadSelect method. Note that this method will load all provided subSelect queries in one single database query.

$book->loadSelect('last_chapter_id', 'latest_version');

If you haven't eager-loaded these subSelect queries in a model, you can still access them as attributes. The first time you access them, They will cause a new database query but the following times they will be available in the model's attributes.

$book->last_chapter_id;
$book->latest_version;

Finally, you can gloabally eager-load these subSelect queries by setting up the withSelect property on the Eloquent model.

class Book extends Model
{
    use AddSubSelects;

    public $withSelect = ['last_chapter_id', 'latest_version'];
}

统计信息

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

GitHub 信息

  • Stars: 32
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固