# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://waad-mawlood.gitbook.io/model-metadata/basics/markdown-1/sync-metadata.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
