korridor/laravel-has-many-sync

Laravel 多对一同步

3.0.0 2024-03-01 12:59 UTC

This package is auto-updated.

Last update: 2024-09-06 12:12:07 UTC


README

Latest Version on Packagist License GitHub Workflow Lint GitHub Workflow Tests Codecov

允许为 Laravel 多对一关系同步方法。

注意

查看solidtime - 现代开源时间追踪器,网址:solidtime.io

安装

您可以使用以下命令通过 composer 安装此包

composer require korridor/laravel-has-many-sync

如果您想使用此包与较旧的 Laravel/PHP 版本,请安装 1.* 版本。

composer require korridor/laravel-has-many-merged "^1"

警告:1.* 版本使用不同的命名空间!

要求

此包已针对以下 Laravel 和 PHP 版本进行测试

  • 10.* (PHP 8.1, 8.2, 8.3)
  • 11.* (PHP 8.2, 8.3)

使用方法

设置 HasMany 关系

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

您可以通过以下方式访问同步方法

$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
]);

同步方法接受一个数组,该数组包含放置在中间表中的数据。任何不在给定数组中的数据都将从中间表中删除。因此,在此操作完成后,中间表中仅存在给定数组中的数据。

不删除的同步

如果您不想删除现有数据,可以在同步方法的第二个参数中传递 false 值。

$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
], false);

不属于 hasMany 关系的 IDs 的行为

如果相关数据中的 ID 不存在或不在 hasMany 关系的范围内,则 sync 函数将抛出 ModelNotFoundException。可以使用 $throwOnIdNotInScope 属性修改此行为。默认情况下,此值为 true。如果设置为 false,则 sync 函数将忽略 ID 而不是抛出异常。

$customer->contacts()->sync([
    [
        'id' => 7, // ID that belongs to a different customer than `$customer`
        'name' => 'Peter',
        'phone_number' => '321',
    ],
    [
        'id' => 1000, // ID that does not exist
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
], throwOnIdNotInScope: false);

控制器中的示例使用

class CustomersController extends Controller
{
    /**
     * Update the specified resource in storage.
     *
     * @param  CustomerRequest  $request
     * @param  Customer $customer
     * @return \Illuminate\Http\Response
     */
    public function update(CustomerRequest $request, Customer $customer)
    {
        DB::transaction(function () use ($customer, $request) {
            $customer->update($request->all());
            $customer->contacts()->sync($request->get('contacts', []));
        });

        return redirect()->route('customers.index');
    }
}

贡献

我欢迎建议和贡献。只需创建一个问题或拉取请求。

本地 docker 环境

“docker” 文件夹包含用于开发的本地 docker 环境。docker 工作空间已安装 composer 和 xdebug。

docker-compose run workspace bash

测试

“composer test” 命令使用 phpunit 运行所有测试。 “composer test-coverage” 命令使用 phpunit 运行所有测试,并在 “coverage” 文件夹中创建覆盖率报告。

代码格式化/代码检查

“composer fix” 命令使用 php-cs-fixer 格式化代码。 “composer lint” 命令使用 phpcs 检查代码。

鸣谢

此包是基于 alfa6661/laravel-hasmany-sync 的分支。

许可协议

此包受 MIT 许可协议(MIT)的许可。有关更多信息,请参阅许可文件