承接 ferrisbane/eloquent-companion 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ferrisbane/eloquent-companion

Composer 安装命令:

composer require ferrisbane/eloquent-companion

包简介

A quintessential package containing companion helper functions to extend the usefulness of Laravel's Eloquent ORM

README 文档

README

A quintessential package containing companion helper functions to extend the usefulness of Laravel's Eloquent ORM

Installation

To install through composer you can either use composer require ferrisbane/eloquent-companion (while inside your project folder) or include the package in your composer.json.

"ferrisbane/eloquent-companion": "0.1.*"

Then run either composer install or composer update to download the package.

To use the package with Laravel 5 add the service provider to the list of service providers in config/app.php.

'providers' => [
    ...

    Ferrisbane\EloquentCompanion\Laravel5ServiceProvider::class

    ...
];

Usage

Once the service provider has been added to your projects provider list the companion helpers are ready to use.

withWhereHas

The withWhereHas helper is useful if you wish to run a ->whereHas() query on your model and also want to eager load the same relationship without writing the same query in the ->with() function. Using ->withWhereHas() keeps your code clean and DRY!

For example if you have a user model and want to return only users that have paid order AND also eager load the paid orders:

User::where('active', true)
    ->withWhereHas('orders', function($query) {
        $query->where('paid', true);
    })
    ->get();

As a comparison in standard Eloquent you would have to write the query out twice, which can become messy for larger queries and changes to one query will have to be replicated again:

User::where('active', true)
    ->whereHas('orders', function($query) {
        $query->where('paid', true);
    })
    ->with([
        'orders' => function($query) {
            $query->where('paid', true);
        }
    ])
    ->get();

toQuery

When writing Eloquent queries it can be useful to see what SQL query it will run. Eloquent provides the ->toSql() function to help you with that, however any bindings wont be populated in the output. The ->toQuery() function is here to help!

User::where('email', 'john@example.com')
    ->toQuery();

Taking a look at the above query the standard ->toSql() function would return: SELECT * FROM users WHERE email = ?. We cant just copy and paste that output into our favorite database client.

However with the ->toQuery() function we can, as it will output: SELECT * FROM users where email = 'john@example.com'. This is extremely useful when debugging those big queries that can contain 10+ bindings.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固