定制 moneo/laravel-morphmap 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

moneo/laravel-morphmap

Composer 安装命令:

composer require moneo/laravel-morphmap

包简介

Custom morphMap support for Laravel Framework

README 文档

README

Laravel Custom Morph Map

Tests Latest Stable Version License

Use different morph maps for different relationships on the same model.

By default, Laravel's Relation::morphMap() applies globally to all polymorphic relationships. This package lets you define a custom morph map per relationship, so different polymorphic relations on the same model can use different type aliases.

Requirements

Version PHP Laravel
2.x ^8.1 10, 11, 12
1.x >=8.0 8, 9, 10, 11

Installation

composer require moneo/laravel-morphmap

No service provider registration is needed. Just use the trait.

Usage

Add the HasCustomMorphMap trait to your model and define the $customMorphMap property in your constructor.

The array keys are the related model's fully qualified class name, and the values are the morph type alias you want stored in the database for that relationship:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Moneo\LaravelMorphMap\Database\Eloquent\Concerns\HasCustomMorphMap;

class Post extends Model
{
    use HasCustomMorphMap;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        // Define custom morph types per related model.
        // Key: related model class, Value: morph type alias
        $this->customMorphMap = [
            Category::class => 'post',
        ];

        // Optional: override the default morph type for all other relationships.
        // If not set, defaults to the fully qualified class name (static::class).
        // $this->defaultMorphType = 'post';
    }

    /**
     * Tags relationship — no custom mapping defined for Tag,
     * so the default morph type (App\Models\Post) will be used.
     */
    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

    /**
     * Categories relationship — custom mapping defined above,
     * so 'post' will be stored as the morph type.
     */
    public function categories(): MorphToMany
    {
        return $this->morphToMany(Category::class, 'categoryable');
    }
}

Inverse Relationships (morphedByMany)

The trait also supports morphedByMany for inverse polymorphic many-to-many relationships:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Moneo\LaravelMorphMap\Database\Eloquent\Concerns\HasCustomMorphMap;

class Category extends Model
{
    use HasCustomMorphMap;

    public function posts(): MorphToMany
    {
        return $this->morphedByMany(Post::class, 'categoryable');
    }

    public function videos(): MorphToMany
    {
        return $this->morphedByMany(Video::class, 'categoryable');
    }
}

How It Works

The trait uses Laravel's initializeHasCustomMorphMap() convention to automatically set the default morph type when the model is constructed. No need to call parent::__construct() before setting properties -- the trait handles initialization automatically.

When you call morphToMany() or morphedByMany(), the trait:

  1. Looks up the related model class in your $customMorphMap array
  2. If found, registers that alias in Laravel's global morph map
  3. If not found, uses the $defaultMorphType (which defaults to the model's FQCN)
  4. Delegates to the parent relationship method

Example Scenario

Suppose your taggables table uses fully qualified class names, but your categoryables table uses short aliases:

taggables

tag_id taggable_id taggable_type
1 1 App\Models\Post
2 1 App\Models\Video

categoryables

category_id categoryable_id categoryable_type
1 1 post
2 1 video

With this package, both table structures work seamlessly on the same model.

Testing

composer test

Static Analysis

composer analyse

Code Style

# Check style
composer format:check

# Fix style
composer format

Contributing

Contributions are always welcome! Please see CONTRIBUTING.md for details.

Thanks to all of our contributors!

License

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

moneo/laravel-morphmap 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 53
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

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