定制 moneo/laravel-morphmap 二次开发

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

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

moneo/laravel-morphmap

最新稳定版本:1.0.4

Composer 安装命令:

composer require moneo/laravel-morphmap

包简介

Custom morphMap support for Laravel Framework

README 文档

README

Laravel Custom Morph Map

This package provides use different morphMap for different relationships.

If your polymorphic relation structures does not satisfy with each other, this package lets you use custom map for each relation definition.

PS: By default, Laravel does not support custom mapping for each relation.

Installation

composer require moneo/laravel-morphmap

Usage

class Post extends Model
{
    // 🚀 Add HasCustomMorphMap trait!
    use HasCustomMorphMap;

    public function __construct(array $attributes = [])
    {
        // 👋 Custom definition for Category relation!    
        $this->customMorphMap = [
            Category::class => 'post',
        ];
        
        // 👋 Default for all others! (__CLASS__ definition is default. You don't need to add this.)
        $this->defaultMorphType = Post::class;

        parent::__construct($attributes);
    }

    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

    public function categories(): MorphToMany
    {
        return $this->morphToMany(Category::class, 'categoryable');
    }
}

This usage example covers the example data below.

Example Case:

Assume you use these tables in your project.

posts
    id - integer
    name - string
 
videos
    id - integer
    name - string
 
tags
    id - integer
    name - string
    
categories
    id - integer
    name - string
 
taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string
    
categoryables
    category_id - integer
    categoryable_id - integer
    categoryable_type - string

Example Data:

posts

id name
1 Easy Seralization in Doctrine

videos

id name
1 Beyond Controllers in Laravel Projects

tags

id name
1 PHP
2 Laravel
3 Doctrine

categories

id name
1 Articles
2 Video Tutorials

taggables

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

categoryables

category_id categoryable_id categoryable_type
1 1 post
3 1 post
1 1 video
2 1 video

Contributing

Contributions are always welcome, thanks to all of our contributors!

统计信息

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

GitHub 信息

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

其他信息

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