定制 f-liva/laravel-hasmany-sync 二次开发

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

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

f-liva/laravel-hasmany-sync

最新稳定版本:v1.2.1

Composer 安装命令:

composer require f-liva/laravel-hasmany-sync

包简介

Sync Laravel HasMany relationships with ease

README 文档

README

Latest Version on Packagist License Codecov TravisCI StyleCI

With this package you will be able to synchronize your HasMany relationships just as you normally would for BelongsToMany relationships. The usage is the same, follow the official Laravel documentation.

Thanks to korridor/laravel-has-many-sync for the original idea. This package reconstructs the mechanism using the original Laravel approach and operation.

Installation

You can install the package via composer with following command:

composer require f-liva/laravel-hasmany-sync

Usage

Configure your HasMany relationship as you normally would

class Customer extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function contacts()
    {
        return $this->hasMany(CustomerContact::class);
    }
}

Synchronize the relationship as you normally would in a BelongsToMany relationship

$customer->contacts()->sync([1, 2, 3]);

// Or with attributes...

$customer->contacts()->sync([1 => ['name' => 'Foo'], 2 => ['name' => 'Bar'], 3]);

The sync method accepts the same parameters described in Eloquent Relationships - Syncing Relations

You can also synchronize the relationship without knowing the identifiers of the related records. In this case, specify in the call the syncRelatedKey: false parameter.

$customer->contacts()->sync([1, 2, 3], syncRelatedKey: false);

// Or with attributes...

$customer->contacts()->sync([['name' => 'Foo'], ['name' => 'Bar'], ...]);

Contributing

I am open for suggestions and contributions. Just create an issue or a pull request.

Testing

The composer test command runs all tests with phpunit. The composer test-coverage command runs all tests with phpunit and creates a coverage report into the coverage folder.

Codeformatting/Linting

The composer fix command formats the code with php-cs-fixer. The composer lint command checks the code with phpcs.

License

This package is licensed under the MIT License (MIT). Please see license file for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-15