承接 masterfri/laravel-smart-relations 相关项目开发

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

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

masterfri/laravel-smart-relations

Composer 安装命令:

composer require masterfri/laravel-smart-relations

包简介

Work with relations as simple as with attributes.

README 文档

README

This library allows to work with relations in the way as if they were attributes. To make a relationship, you only need to assign a value (or values in case of mass assignment) to model and create/save it.

For now the following relation types are supported: BelongsTo, BelongsToMany, HasOne, HasMany, MorphTo, MorphToMany, MorphOne, MorphMany.

Installation

composer require masterfri/laravel-smart-relations

Examples

use Masterfri\SmartRelations\SmartRelations;

class Library extends Model
{
    // To enable smart relations add the following trait in your model
    use SmartRelations;
    
    // Relations can be listed in $fillable property to enable mass assignment
    protected $fillable = ['name', 'books', 'readers'];
    
    // If you want children records to be deleted along with parent record
    // you can list required relation names in $cascade_delete property.
    // For example, when library instance is deleted all related books
    // will be deleted as well, and readers will be detached (not deleted, since 
    // it is BelongsToMany relationship)
    protected $cascade_delete = ['books', 'readers'];
    
    public function books()
    {
        return $this->hasMany(Book::class);
    }
    
    public function readers()
    {
        return $this->belongsToMany(Reader::class, 'subscriptions');
    }
}

class Book extends Model
{
    use SmartRelations;
    
    protected $fillable = ['title', 'library'];
    
    public function library()
    {
        return $this->belongsTo(Library::class);
    }
    
    public function card()
    {
        return $this->hasOne(BookCard::class);
    }
}

class Reader extends Model
{
    use SmartRelations;
    
    protected $fillable = ['name'];
}

class BookCard extends Model
{
    use SmartRelations;
    
    protected $fillable = ['book', 'reader'];
    
    public function book()
    {
        return $this->belongsTo(Book::class);
    }
    
    public function reader()
    {
        return $this->belongsTo(Reader::class);
    }
}

// HasMany relation example
// =========================
$library = Library::create([
    'name' => 'Central Library',
    // Related records can be passed as array or model instance
    // If instances does not exist in database, they will be created
    // You also can pass ID of existing record to attach it
    'books' => [
        ['title' => 'First Book'], 
        new Book(['title' => 'Second Book']),
        $existingBook,
        123,
    ],
]);
// or 
$library->books = [
    ['title' => 'First Book'], 
    new Book(['title' => 'Second Book']),
    $existingBook,
    123,
];
$library->save();

// HasOne relation example
// =========================
$book = Book::create([
    'title' => 'Third book',
    'card' => new BookCard(),
    // or simply
    // 'card' => [],
]);

// BelongsTo relation example
// =========================
$book = Book::create([
    'title' => 'Third book',
    // In that way model can be associated with parent record
    'library' => $library,
]);
// or
$book->library = $library;
$book->save();

// BelongsToMany relation example
// =========================
$reader1 = Reader::create(['name' => 'John']);
$reader2 = Reader::create(['name' => 'Jack']);

// In case of many to many relation you can pass model instance/ID to make relationship 
$library->readers = [$reader1, $reader2->id];
$library->save();

// Update data on related records
// =========================
// If you pass primary key in the data, related records will be loaded
// from database, and their attributes will be updated. But bear in mind
// that related records which are not listed in the array will be deleted,
// because all children records will be replaced with new data.
$library->books = [
    ['id' => 1, 'title' => 'First Book New Name'], 
    ['id' => 2, 'title' => 'Second Book New Name'], 
];

masterfri/laravel-smart-relations 适用场景与选型建议

masterfri/laravel-smart-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.61k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2018 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-19