定制 jwohlfert23/laravel-sync-relations 二次开发

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

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

jwohlfert23/laravel-sync-relations

最新稳定版本:5.0.0

Composer 安装命令:

composer require jwohlfert23/laravel-sync-relations

包简介

Package to sync nested relations from request data

README 文档

README

Laravel Sync Relations

Add this to your models:

use Jwohlfert23/LaravelSyncRelations/SyncableTrait;
...

class Post extends Model {
    use SyncableTrait;
}

And you can use it in your controllers like so:

public function update($id) {
    $post = Post::find($id);
    
    $post->saveAndSync(request()->input(), ['tags']);
}

Let's assume the following

  • Post model has a "hasMany" relationship with tags.
  • The request data looks like:
{
  "title": "My Post Title",
  "tags": [{
    "id": 1,
    "name": "My First Tag"
  }, {
     "name": "My Second Tag"
  }]
}

By running saveAndSync, it will iterate over the 2 tags provided in the request. Because the first one already exists, it will update that one and set the name to "My First Tag". The second one doesn't exist aleady so it will create it using the name "My Second Tag". After this controller processeed this request, you could run the following:

Post::find($id)->tags->pluck('name')

and get this:

["My First Tag", "My Second Tag"]

This package also works with "belongsToMany" and "belongsTo" relationship types. However, for those, it will not update/create the nested objects. It will only associate the existing models with the parent model (in other words, for these relationship, you must provide the primary key for each child).

Additionally, you could run saveAndSync(request()->input(), ['comments.author']) to do a nested sync.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-19