> For the complete documentation index, see [llms.txt](https://waad-mawlood.gitbook.io/model-metadata/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://waad-mawlood.gitbook.io/model-metadata/basics/markdown-1/sync-metadata.md).

# Sync Metadata

## 🔥 `syncMetadata`&#x20;

* Sync metadata records by replacing all existing ones with new data.

```php
⚠️ cases the operation of this method:

/* 
* This method performs an atomic operation that:
* 1. Deletes all existing metadata records for the model
* 2. Creates new metadata records from the provided data
* 
* The operation will fail and return false if:
* - The provided data is not a valid nested metadata structure
* - The deletion of existing records fails
* 
* If the provided metadata is empty:
* - All existing metadata will be deleted
* - The method will return true
*/ 

```

```php
// Syntax
$model->syncMetadata(array|Collection $metadata): bool
```

#### Example Usage

```php

// Using nested array or Collection return all true
$model->syncMetadata([
    [
        'language' => 'Arabic',
        'theme' => 'auto',
    ],
    [
        'language' => 'Spanish',
        'theme' => 'dark',
    ],
]);

$model->syncMetadata(collect(
    [
        'theme' => 'dark',
        'views' => 767
    ],
    [
        'theme' => 'light',
        'views' => 543
    ],
));

$model->syncMetadata(collect(
    collect([
        'theme' => 'dark',
        'views' => 767
    ]),
    collect([
        'theme' => 'light',
        'views' => 543
    ]),
))



// if metadata is not nested (array, collection) Or empty
$model->syncMetadata([]));  // True;  delete all oldest metadata 
$model->syncMetadata([[]]));  // False; nested but not filled data
$model->syncMetadata([null]));  // False;
$model->syncMetadata(['']));  // False;
$model->syncMetadata(collect([])));  // True;  delete all oldest metadata
$model->syncMetadata([collect([])]));  // False;
$model->syncMetadata(collect([collect([])])));  // False;
$model->syncMetadata(collect([collect([''])])));  // False;

```

| Parameters                               | Data Type                                              |
| ---------------------------------------- | ------------------------------------------------------ |
| <mark style="color:red;">metadata</mark> | <mark style="color:purple;">`array, Collection`</mark> |

{% hint style="info" %} <mark style="color:green;">Return Type ⇒</mark> <mark style="color:orange;">Boolean</mark>
{% endhint %}
