PHPackages                             illuminatech/sync-many-attribute - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. illuminatech/sync-many-attribute

ActiveLibrary[Database &amp; ORM](/categories/database)

illuminatech/sync-many-attribute
================================

Allows control over Eloquent many-to-many relation via array attribute

1.1.7(1mo ago)166.7kBSD-3-ClausePHPCI failing

Since Feb 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/illuminatech/sync-many-attribute)[ Packagist](https://packagist.org/packages/illuminatech/sync-many-attribute)[ GitHub Sponsors](https://github.com/klimov-paul)[ Patreon](https://www.patreon.com/klimov_paul)[ RSS](/packages/illuminatech-sync-many-attribute/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (10)Used By (0)

 [ ![](https://avatars1.githubusercontent.com/u/47185924) ](https://github.com/illuminatech)

Sync Eloquent Many-to-Many via Array Attribute
==============================================

[](#sync-eloquent-many-to-many-via-array-attribute)

This extension allows control over Eloquent many-to-many relations via array attributes.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/5800732a6cc83081bc50f0e76bb29b3a68f10892d8fecd87f0e3134fb1f550bc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696c6c756d696e61746563682f73796e632d6d616e792d6174747269627574652e737667)](https://packagist.org/packages/illuminatech/sync-many-attribute)[![Total Downloads](https://camo.githubusercontent.com/7d936f730b944ceb591fa732cbf8801671d3fa1f208648fd763d451a7cdedb41/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696c6c756d696e61746563682f73796e632d6d616e792d6174747269627574652e737667)](https://packagist.org/packages/illuminatech/sync-many-attribute)[![Build Status](https://github.com/illuminatech/sync-many-attribute/workflows/build/badge.svg)](https://github.com/illuminatech/sync-many-attribute/actions)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist illuminatech/sync-many-attribute

```

or add

```
"illuminatech/sync-many-attribute": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension allows control over Eloquent many-to-many relations via array attributes. Each such attribute matches particular `BelongsToMany` relation and accepts array of related model IDs. Relations will be automatically synchronized during model saving.

> Note: in general such approach makes a little sense, since Eloquent already provides fluent interface for many-to-many relation synchronization. However, this extension make come in handy while working with 3rd party CMS like [Nova](https://nova.laravel.com), where you have a little control over model saving and post processing. Also it may simplify controller code, removing relation operations in favor to regular attribute mass assignment.

In order to use the feature you should add `\Illuminatech\SyncManyAttribute\SyncManyToManyAttribute` trait to your model class and declare `syncManyToManyAttributes()` method, defining attributes for relation synchronization. This method should return an array, which each key is the name of the new virtual attribute and value is the name of the relation to be synchronized.

For example:

```
