blasttech/eloquent-related-plus 问题修复 & 功能扩展

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

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

blasttech/eloquent-related-plus

Composer 安装命令:

composer require blasttech/eloquent-related-plus

包简介

Adds search and order functionality to Laravel Eloquent related models

README 文档

README

Latest Version Build Status Software License StyleCI Quality Score Total Downloads

This package provides a trait that adds extra ways to order and search complex models. With Eloquent, when relations are used (eg. HasOne, HasMany, BelongsTo) the primary model can't be sorted using a field in a related table.

For example, you might have a list of Customers with a one-to-one relation (HasOne) to a Contacts model for the Customer contact.

class Customer extends Eloquent
{
    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}

If you had a table of customers and wanted to sort it by Contact name, you nomrally wouldn't be able to in Laravel using the relation above, but you could with this package.

Installation

This package can be installed through Composer.

$ composer require blasttech/eloquent-related-plus

Usage

To add complex order and search behaviour to your model you must:

  1. specify that the model will conform to Blasttech\EloquentRelatedPlus\RelatedPlusInterface
  2. use the trait Blasttech\EloquentRelatedPlus\RelatedPlusTrait

Using the earlier example:

use App\Contact;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class Customer extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;
    
    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}

modelJoin

Use this scope to add a join for a related model.

  • modelJoin($relation_name, $operator = '=', $type = 'left', $where = false, $related_select = true)

Example

namespace App\Http\Controllers;

use App\Customer;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->get(); 
    }
    
    ...
}

This will add a left join to the Contacts model using the Contacts() relation, using 'where' instead of 'on' and include all the fields from the model in the select.

$relation_name

The name of the relation. Only BelongsTo, HasOne, HasMany or HasOneOrMany relations will work.

$operator

The operator used to join the related table. This will default to '=' but any of the standard Laravel join operators can be used.

$type

The type of join. This will default to 'left' but any of the standard Laravel join types are allowed.

$where

The method of joining the table. The default (false) uses an 'on' statement but if true, a where statement is used.

$related_select

The $related_select option determines if the fields in the joined table will be included in the query. If true, the field names will be in the format 'table_name.column_name', for example, 'customer.contact_name' with the table name and period (.) included in the field name. This is to allow fields from joined tables to be used when they have the same column names.

orderByCustom

  • orderByCustom($orderField, $dir, $orderFields = null, $orderDefaults = null)

Example

use App\Customer;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->orderByCustom('contact_name'); 
    }
    
    ...
}

This will add a left join to the Contacts model using the Contacts() relation and then sort it by the contact_name.

search

Example

use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class MyModel extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;
    
    public function getContact($contact_name)
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->search($contact_name); 
    }
    
    ...
}

This will add a left join to the Contacts model using the Contacts() relation, and search for $contact_name.

License

The MIT License (MIT). Please see License File for more information.

Credits

The example shown in http://laravel-tricks.com/tricks/automatic-join-on-eloquent-models-with-relations-setup was used as a basis for the modelJoin and relationJoin scopes.

blasttech/eloquent-related-plus 适用场景与选型建议

blasttech/eloquent-related-plus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.92k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「model」 「laravel」 「join」 「eloquent」 「orderby」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 blasttech/eloquent-related-plus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 blasttech/eloquent-related-plus 我们能提供哪些服务?
定制开发 / 二次开发

基于 blasttech/eloquent-related-plus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 10
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-28