clickbar/laravel-custom-relations 问题修复 & 功能扩展

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

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

clickbar/laravel-custom-relations

Composer 安装命令:

composer require clickbar/laravel-custom-relations

包简介

This package provides functionality for custom relations in laravel

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel provides some pretty good relations from scratch. If those relations do not fit the need, the community has provided a lot of other relation packages. Especially staudenmeir did a lot if nice work on that end. However, in our projects we encountered the need of some really custom relations that could easily be expressed with an eloquent query, but not with default relations. Therefore, we created this package to give you the full control over your relations.

Introduction

Sometimes it needs to be more custom. This package extends the default Laravel Relations with Relations that can be described by a query.

For explanation purposes we consider the following model with a simple concatenated relation use case. (for cases like this you should also have a look eloquent-has-many-deep from staudenmeir). Even the example is quite simple, it should be able to represent the huge amount of possibilities that comes with custom relations. model chain

Let's explorer the connection between Client and Task in the default Laravel way:

$client = $task->order->project->client;
$tasks = $client->projects->flatMap(fn(Project $project) => $project->order->flatMap(fn(Order $order) => $order->tasks));

Wouldn't it be cool to do stuff like this with only 1 single database query?

$client = $task->client;
$tasks = $client->tasks;

With Custom Relations you can do this with only one single Database Query.

Installation

You can install the package via composer:

composer require clickbar/laravel-custom-relations

Preparing the Model

In order to use the Custom Relations, you must use the HasCustomRelation trait:

use Clickbar\LaravelCustomRelations\Traits\HasCustomRelation;


class Client extends Model{
    
    use HasCustomRelation;
    
    ...
}

Writing the Relation

Like you know from Laravel, relations can return a collection of models or just one model. Therefore, this package has two Different Relations CustomRelation and CustomRelationSingle.

Let's look at our two examples from above:

class Task extends Model {

    use HasCustomRelation;

    public function client(): CustomRelationSingle {
        return $this->customRelationSingle(
            Client::class,
            function ($query) {
                $query
                    ->join('projects', 'clients.id', 'client_id')
                    ->join('orders', 'projects.id', 'project_id')
                    ->join('tasks', 'orders.id', 'order_id');
            },
        );
    }
}
class Client extends Model {

    use HasCustomRelation;

    public function tasks(): CustomRelation {
        return $this->customRelation(
            Task::class,
            function ($query) {
                $query
                    ->join('orders', 'orders.id', 'order_id')
                    ->join('projects', 'projects.id', 'project_id')
                    ->join('clients', 'clients.id', 'client_id');
            },
        );
    }
}

Like regular Laravel Relations, the query builder starts from the related model. This results in the following join chains:
$task->client: Client->Projects->Orders->Tasks
$client->tasks: Tasks->Orders->Projects->Client

If you prefer starting the join from the model the relation is defined on, you can use the method with the fromParent suffix:

class Task extends Model {

    use HasCustomRelation;

    public function client(): CustomRelationSingle {
        return $this->customRelationFromParentSingle(
            Client::class,
            function ($query) {
                $query
                    ->join('orders', 'orders.id', 'order_id')
                    ->join('projects', 'projects.id', 'project_id')
                    ->join('clients', 'clients.id', 'client_id');
            },
        );
    }
}
class Client extends Model {

    use HasCustomRelation;

    public function tasks(): CustomRelation {
        return $this->customRelationFromParent(
            Task::class,
            function ($query) {
                $query
                    ->join('projects', 'clients.id', 'client_id')
                    ->join('orders', 'projects.id', 'project_id')
                    ->join('tasks', 'orders.id', 'order_id');
            },
        );
    }
}

Limitations

Since the query might introduce a lot of joins, some methods known from Laravel Relations are not available:

  • make
  • create
  • update
  • forceCreate
  • forceDelete

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

clickbar/laravel-custom-relations 适用场景与选型建议

clickbar/laravel-custom-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 8, 最近一次更新时间为 2024 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 clickbar/laravel-custom-relations 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-22