Skip to content

A free open source IT asset/license management system

Fixes javascript error when custom fields exist but do not belong to any fieldsets or models

snipeccb7556281eb
diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php
index ff47daa28c..3f60a613e2 100644
--- a/app/Presenters/AssetPresenter.php
+++ b/app/Presenters/AssetPresenter.php
@@ -184,7 +184,17 @@ class AssetPresenter extends Presenter
],
];
- $fields = CustomField::all();
+ // This looks complicated, but we have to confirm that the custom fields exist in custom fieldsets
+ // *and* those fieldsets are associated with models, otherwise we'll trigger
+ // javascript errors on the bootstrap tables side of things, since we're asking for properties
+ // on fields that will never be passed through the REST API since they're not associated with
+ // models. We only pass the fieldsets that pertain to each asset (via their model) so that we
+ // don't junk up the REST API with tons of custom fields that don't apply
+
+ $fields = CustomField::whereHas('fieldset', function ($query) {
+ $query->whereHas('models');
+ })->get();
+
foreach ($fields as $field) {
$layout[] = [
"field" => 'custom_fields.'.$field->convertUnicodeDbSlug(),