PHPackages                             denizgolbas/eloquent-hasduplicate-attirbutes - 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. denizgolbas/eloquent-hasduplicate-attirbutes

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

denizgolbas/eloquent-hasduplicate-attirbutes
============================================

Laravel Eloquent trait for duplicating attributes from related models

v1.0.0(4mo ago)00MITPHPPHP ^8.1CI passing

Since Dec 29Pushed 4mo agoCompare

[ Source](https://github.com/denizgolbas/eloquent-hasduplicate-attirbutes)[ Packagist](https://packagist.org/packages/denizgolbas/eloquent-hasduplicate-attirbutes)[ RSS](/packages/denizgolbas-eloquent-hasduplicate-attirbutes/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Eloquent HasDuplicateAttributes
===============================

[](#eloquent-hasduplicateattributes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7c39d0fe9cb4dce1a3f74dc5afa079fce3c9d6bdeb97824f803179e121435317/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656e697a676f6c6261732f656c6f7175656e742d6861736475706c69636174652d617474697262757465732e7376673f7374796c653d666c61742d737175617265266c6162656c3d5061636b6167697374)](https://packagist.org/packages/denizgolbas/eloquent-hasduplicate-attirbutes)[![Total Downloads](https://camo.githubusercontent.com/e9da3052ab1a7ce4345f5093e3fca8193f2874133dba9202eb7821254c72bcaf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656e697a676f6c6261732f656c6f7175656e742d6861736475706c69636174652d617474697262757465732e7376673f7374796c653d666c61742d737175617265266c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/denizgolbas/eloquent-hasduplicate-attirbutes)[![Tests](https://github.com/denizgolbas/eloquent-hasduplicate-attirbutes/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/denizgolbas/eloquent-hasduplicate-attirbutes/actions/workflows/tests.yml)[![License](https://camo.githubusercontent.com/de977566245104725bdebcd2a11947a0a34d997cb8e987e7458d478548bc162a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64656e697a676f6c6261732f656c6f7175656e742d6861736475706c69636174652d617474697262757465732e7376673f7374796c653d666c61742d737175617265266c6162656c3d4c6963656e7365)](https://packagist.org/packages/denizgolbas/eloquent-hasduplicate-attirbutes)

A powerful Laravel Eloquent trait that automatically duplicates attributes from related models when creating or updating records. This package simplifies the process of copying data from parent or related models to child models, with flexible override options.

Features
--------

[](#features)

- 🚀 **Automatic Attribute Copying**: Automatically copies attributes from related models on `creating`, `updating`, and `saving` events
- ⚙️ **Flexible Override Control**: Choose whether to always override or only copy when fields are empty
- 🔄 **Relation Support**: Works with both single model relations and collections
- 🎯 **Selective Copying**: Prevent copying for specific save operations when needed
- ✅ **Well Tested**: Comprehensive test suite with multiple scenarios

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

[](#installation)

You can install the package via Composer:

```
composer require denizgolbas/eloquent-hasduplicate-attirbutes
```

The package will automatically register its service provider.

Basic Usage
-----------

[](#basic-usage)

### Step 1: Use the Trait

[](#step-1-use-the-trait)

Add the `HasDuplicateAttributes` trait to your Eloquent model:

```
use Denizgolbas\EloquentHasduplicateAttirbutes\HasDuplicateAttributes;
use Illuminate\Database\Eloquent\Model;

class CustomerSlip extends Model
{
    use HasDuplicateAttributes;

    // ... your model code
}
```

### Step 2: Define the Duplicates Configuration

[](#step-2-define-the-duplicates-configuration)

Define which attributes should be copied from which relations using the `$duplicates` array:

```
protected array $duplicates = [
    // Format: 'local_field' => ['related_field', 'relation_method', override_flag]
    'name' => ['name', 'customer'],           // Always copy customer name
    'code' => ['code', 'customer', false],    // Only copy if code is empty
];
```

### Step 3: Define the Relation

[](#step-3-define-the-relation)

Make sure you have the relation method defined in your model:

```
public function customer()
{
    return $this->belongsTo(Customer::class);
}
```

Configuration Options
---------------------

[](#configuration-options)

### Override Flag

[](#override-flag)

The third parameter in the configuration array controls the override behavior:

- **`true` (default)**: Always copy the value from the related model, even if the local field already has a value
- **`false`**: Only copy the value if the local field is empty (null, empty string, etc.)

### Configuration Format

[](#configuration-format)

```
protected array $duplicates = [
    'local_field_name' => [
        'related_field_name',  // The field name in the related model
        'relation_method',      // The relation method name (e.g., 'customer', 'source')
        true                    // Optional: override flag (default: true)
    ],
];
```

Detailed Examples
-----------------

[](#detailed-examples)

### Example 1: Customer Slip with Customer Information

[](#example-1-customer-slip-with-customer-information)

This example shows how to automatically copy customer information to a slip when creating it:

```
