Skip to content

Commit 052098b

Browse files
authored
Merge pull request #75 from dhcmega/master
Set model attribute for preventing metas to being populated on toArray()
2 parents ab3a9aa + 8074f2b commit 052098b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ $post = Post::meta()
285285

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

288-
```
288+
```php
289289
$post = Post::with(['metas'])->get();
290290
```
291+
292+
#### Prevent metas attribute from being populated
293+
294+
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.
295+
You can also use it on eloquent relations.
296+
297+
```php
298+
/* Post model */
299+
public $hideMeta = true; // Do not add metas to array
300+
```

src/Kodeine/Metable/Metable.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,11 @@ protected function getMetaClass()
298298
*/
299299
public function toArray()
300300
{
301-
return array_merge(parent::toArray(), [
302-
'meta_data' => $this->getMeta()->toArray(),
303-
]);
301+
return $this->hideMeta ?
302+
parent::toArray() :
303+
array_merge(parent::toArray(), [
304+
'meta_data' => $this->getMeta()->toArray(),
305+
]);
304306
}
305307

306308
/**

0 commit comments

Comments
 (0)