-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from waadmawlood/2.x
migrate to new version 2.x more food of methods
- Loading branch information
Showing
16 changed files
with
1,274 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,17 @@ | |
|
||
Laravel Model Metadata is a package designed to manage metadata with JSON support for multiple data types. It allows you to easily attach, manage, and query metadata on your Laravel models using the `HasManyMetadata` or `HasOneMetadata` traits. | ||
|
||
## ✨ Requirements | ||
# 📚 Documentation | ||
|
||
- PHP 8.1 or higher | ||
- Laravel 10.0 or higher | ||
For detailed documentation, including usage examples and best practices, please refer to the [Documentation](https://waad-mawlood.gitbook.io/model-metadata). | ||
|
||
# ✨ Requirements | ||
|
||
- PHP 8.0 or higher | ||
- Laravel framework 9.30.1 or higher | ||
- JSON extension enabled | ||
|
||
## 💼 Installation | ||
# 💼 Installation | ||
|
||
1. Install the package using Composer: | ||
```bash | ||
|
@@ -27,46 +31,78 @@ Laravel Model Metadata is a package designed to manage metadata with JSON suppor | |
php artisan migrate | ||
``` | ||
|
||
## 🎈 Usage | ||
# 🎈 Usage | ||
|
||
### 🔥 HasManyMetadata Trait | ||
## 🔥 HasOneMetadata Trait | ||
|
||
This trait allows a model to have multiple metadata records. Add the trait to your model: | ||
Add the HasOneMetadata trait to your model to enable a single metadata record: | ||
|
||
```php | ||
use Waad\Metadata\Traits\HasManyMetadata; | ||
use Waad\Metadata\Traits\HasOneMetadata; | ||
|
||
class Post extends Model | ||
class Company extends Model | ||
{ | ||
use HasManyMetadata; | ||
use HasOneMetadata; // <--- Add this trait to your model | ||
} | ||
``` | ||
|
||
Available methods: | ||
#### Some methods: | ||
|
||
#### Creating Metadata | ||
```php | ||
// Create metadata with array | ||
$post->createMetadata(['key' => 'value', 'another_key' => 'another_value']); | ||
// Create metadata with array (only works if no metadata exists) | ||
$company->createMetadata(['key' => 'value', 'another_key' => 'another_value']); | ||
|
||
// Create metadata with collection | ||
$post->createMetadata(collect(['key' => 'value'])); | ||
$company->createMetadata(collect(['key' => 'value'])); | ||
|
||
// Update existing metadata | ||
$company->updateMetadata(['new_key' => 'new_value']); | ||
|
||
// Delete the metadata | ||
$company->deleteMetadata(); | ||
|
||
// Get metadata as array | ||
$metadata = $company->getMetadata(); | ||
|
||
// Get metadata as collection | ||
$metadataCollection = $company->getMetadataCollection(); | ||
``` | ||
|
||
#### Updating Metadata | ||
------------- | ||
|
||
## 🔥 HasManyMetadata Trait | ||
|
||
Add the HasManyMetadata trait to your model to enable multiple metadata records: | ||
|
||
```php | ||
// Update metadata by ID | ||
$post->updateMetadata('metadata_id', ['new_key' => 'new_value']); | ||
use Waad\Metadata\Traits\HasManyMetadata; | ||
|
||
class Post extends Model | ||
{ | ||
use HasManyMetadata; // <--- Add this trait to your model | ||
|
||
// Enabled Append id with content metadata (default) | ||
public $metadataNameIdEnabled = true; | ||
|
||
// Custom Append key of id with metadata (default) | ||
public $metadataNameId = 'id'; | ||
} | ||
``` | ||
see [Configuration Append Id](https://waad-mawlood.gitbook.io/model-metadata/basics/markdown-1/use-in-model) for more details | ||
|
||
#### Some methods: | ||
|
||
#### Deleting Metadata | ||
```php | ||
// Create metadata with array or collection | ||
$post->createMetadata(['key1' => 'value1', 'key2' => 'value2']); | ||
$post->createMetadata(collect(['key1' => 'value1', 'key2' => 'value2'])); | ||
|
||
// Update metadata by ID | ||
$post->updateMetadata('{metadata_id}', ['new_key' => 'new_value']); | ||
|
||
// Delete metadata by ID | ||
$post->deleteMetadata('metadata_id'); | ||
``` | ||
$post->deleteMetadata('{metadata_id}'); | ||
|
||
#### Retrieving Metadata | ||
```php | ||
// Get all metadata objects | ||
$metadata = $post->metadata; | ||
// or | ||
|
@@ -85,62 +121,23 @@ $metadataCollection = $post->getMetadataCollection(); | |
$searchResults = $post->searchMetadata('search_term'); | ||
``` | ||
|
||
### 🔥 HasOneMetadata Trait | ||
|
||
This trait allows a model to have a single metadata record. Add the trait to your model: | ||
|
||
```php | ||
use Waad\Metadata\Traits\HasOneMetadata; | ||
|
||
class Company extends Model | ||
{ | ||
use HasOneMetadata; | ||
} | ||
``` | ||
|
||
Available methods: | ||
|
||
#### Creating Metadata | ||
```php | ||
// Create metadata with array (only works if no metadata exists) | ||
$company->createMetadata(['key' => 'value', 'another_key' => 'another_value']); | ||
|
||
// Create metadata with collection | ||
$company->createMetadata(collect(['key' => 'value'])); | ||
``` | ||
|
||
#### Updating Metadata | ||
```php | ||
// Update existing metadata | ||
$company->updateMetadata(['new_key' => 'new_value']); | ||
``` | ||
------------- | ||
|
||
#### Deleting Metadata | ||
```php | ||
// Delete the metadata | ||
$company->deleteMetadata(); | ||
``` | ||
# 🧪 Testing | ||
|
||
#### Retrieving Metadata | ||
```php | ||
// Get metadata as array | ||
$metadata = $company->getMetadata(); | ||
To run the tests for development, use the following command: | ||
|
||
// Get metadata as collection | ||
$metadataCollection = $company->getMetadataCollection(); | ||
```bash | ||
composer test | ||
``` | ||
|
||
Both traits use JSON casting for the metadata column, allowing you to store complex data structures. The metadata is stored in a polymorphic relationship, making it flexible and reusable across different models. | ||
|
||
|
||
## 👨💻 Contributors | ||
# 👨💻 Contributors | ||
|
||
- **Waad Mawlood** | ||
- Email: [email protected] | ||
- Role: Developer | ||
|
||
|
||
|
||
## 📝 License | ||
# 📝 License | ||
|
||
This package is open-sourced software licensed under the [MIT license](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ public function down(): void | |
{ | ||
Schema::dropIfExists('model_metadata'); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Waad\Metadata\Helpers; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
|
||
class Helper | ||
{ | ||
public function isNullOrStringEmptyOrWhitespaceOrEmptyArray(mixed $value): bool | ||
{ | ||
if (is_null($value)) { | ||
return true; | ||
} | ||
|
||
if (is_string($value) && trim($value) === '') { | ||
return true; | ||
} | ||
|
||
if ($value instanceof Collection) { | ||
$value = $value->toArray(); | ||
} | ||
|
||
return is_array($value) && empty($value); | ||
} | ||
|
||
public function pipMetadataToClearKeyNameId(array|Collection $metadata, string $keyNameId = 'id'): array | ||
{ | ||
$metadata = $metadata instanceof Collection ? $metadata->toArray() : $metadata; | ||
$firstItem = Arr::first($metadata); | ||
|
||
// Check if metadata is nested (array of arrays) | ||
if (filled($firstItem) && is_array($firstItem)) { | ||
return array_map(fn ($item) => Arr::except($item, $keyNameId), $metadata); | ||
} | ||
|
||
return Arr::except($metadata, $keyNameId); | ||
} | ||
|
||
public function isNestedMetadata(array|Collection $metadata): bool | ||
{ | ||
$metadata = $metadata instanceof Collection ? $metadata->toArray() : $metadata; | ||
$firstItem = Arr::first($metadata); | ||
|
||
return filled($firstItem) && is_array($firstItem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.