Skip to content

Commit

Permalink
Merge pull request #75 from dhcmega/master
Browse files Browse the repository at this point in the history
Set model attribute for preventing metas to being populated on toArray()
  • Loading branch information
kodeine authored Apr 22, 2020
2 parents ab3a9aa + 8074f2b commit 052098b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ $post = Post::meta()

When you need to retrive multiple results from your model, you can eager load `metas`

```
```php
$post = Post::with(['metas'])->get();
```

#### Prevent metas attribute from being populated

When you convert a model to an array (or json) and you don't need all meta fields, you can create a model's property to prevent metas from being added to the resulting array.
You can also use it on eloquent relations.

```php
/* Post model */
public $hideMeta = true; // Do not add metas to array
```
8 changes: 5 additions & 3 deletions src/Kodeine/Metable/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,11 @@ protected function getMetaClass()
*/
public function toArray()
{
return array_merge(parent::toArray(), [
'meta_data' => $this->getMeta()->toArray(),
]);
return $this->hideMeta ?
parent::toArray() :
array_merge(parent::toArray(), [
'meta_data' => $this->getMeta()->toArray(),
]);
}

/**
Expand Down

0 comments on commit 052098b

Please sign in to comment.