Skip to content

Commit 3afc59d

Browse files
committed
model set metas hidden on convert to array
1 parent ab3a9aa commit 3afc59d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,13 @@ When you need to retrive multiple results from your model, you can eager load `m
288288
```
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+
```
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)