Skip to content

Commit 933248b

Browse files
author
Cyril Wanner
committed
add config to enable the relation cleanup
1 parent b75e7ec commit 933248b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
composer.phar
33
composer.lock
44
.DS_Store
5-
.phpintel
5+
.phpintel
6+
.idea

config/apihandler.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@
5050

5151
'fulltext_score_column' => '_score',
5252

53+
/*
54+
|--------------------------------------------------------------------------
55+
| Cleanup Relations in the Response
56+
|--------------------------------------------------------------------------
57+
|
58+
| By default, the API Handler returns all loaded relations of the models,
59+
| even if they are not in the '_with' param or explicitly loaded with
60+
| $builder->with(). These unexpected relations can come from custom
61+
| accessor methods or when you access a relation in your code in general.
62+
|
63+
| If you want these to get removed from the response, set this value
64+
| to true so only the ones in the '_with' param or explicitly added with
65+
| $builder->with('relation') get returned.
66+
|
67+
*/
68+
69+
'cleanup_relations' => false,
70+
5371
/*
5472
|--------------------------------------------------------------------------
5573
| Errors

src/Result.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Database\Eloquent\Collection;
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Support\Facades\Response;
6+
use \Illuminate\Support\Facades\Config;
67

78
class Result
89
{
@@ -56,9 +57,17 @@ public function getResponse()
5657
public function getResult()
5758
{
5859
if ($this->parser->multiple) {
59-
$result = $this->cleanupRelationsOnModels($this->parser->builder->get());
60+
$result = $this->parser->builder->get();
61+
62+
if (Config::get('apihandler.cleanup_relations', false)) {
63+
$result = $this->cleanupRelationsOnModels($result);
64+
}
6065
} else {
61-
$result = $this->cleanupRelations($this->parser->builder->first());
66+
$result = $this->parser->builder->first();
67+
68+
if (Config::get('apihandler.cleanup_relations', false)) {
69+
$result = $this->cleanupRelations($result);
70+
}
6271
}
6372

6473
return $result;

0 commit comments

Comments
 (0)