From 8b9aea8874070d3fff237f3fad2dafc6cdd017f3 Mon Sep 17 00:00:00 2001 From: slong753 Date: Wed, 12 Apr 2023 14:46:48 -0500 Subject: [PATCH] very much WIP, but dupe queries reduced but i think this can be cleaned up a bit more --- .../Assets/BulkAssetsController.php | 20 ++++++++++--------- app/Models/CustomField.php | 4 +++- .../custom_fields_form_bulk_edit.blade.php | 20 +++++++++---------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 98494fcb76..8a32dc3e44 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -44,17 +44,19 @@ class BulkAssetsController extends Controller $asset_ids = array_values(array_unique($request->input('ids'))); //custom fields logic for bulk edit - $asset_custom_field = Asset::whereIn('id', $asset_ids)->whereHas('model', function ($query) { + $asset_custom_field = Asset::with('model.fieldset.fields')->whereIn('id', $asset_ids)->whereHas('model', function ($query) { return $query->where('fieldset_id', '!=', null); })->get(); - $models = $asset_custom_field->unique('model_id')->pluck('model'); + $models = $asset_custom_field->unique('model_id'); - $custom_fields = new Collection(); - foreach ($asset_custom_field as $asset_key => $asset) { - $custom_fields->push($asset->model->fieldset->fields); - } - $custom_fields = $custom_fields->flatten()->unique('id'); + ray($asset_custom_field); + ray($models); + // $custom_fields = new Collection(); + // foreach ($models as $asset_key => $asset) { + // $custom_fields->push($asset->model->customFields); + // } + $custom_fields = $asset_custom_field->pluck('model.fieldset.fields')->flatten()->unique('id'); if ($request->filled('bulk_actions')) { switch ($request->input('bulk_actions')) { @@ -75,8 +77,8 @@ class BulkAssetsController extends Controller return view('hardware/bulk') ->with('assets', $asset_ids) ->with('statuslabel_list', Helper::statusLabelList()) - ->with('custom_fields', $custom_fields) - ->with('models', $models); + // ->with('custom_fields', $custom_fields) + ->with('models', $models->pluck('model')); } } diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php index ea62c4fa65..46a33548dd 100644 --- a/app/Models/CustomField.php +++ b/app/Models/CustomField.php @@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Validation\Rule; use Schema; use Watson\Validating\ValidatingTrait; - +// use App\Models\SnipeModel; class CustomField extends Model { use HasFactory; @@ -181,6 +181,8 @@ class CustomField extends Model public function assetModels() { return $this->fieldset()->with('models')->get()->pluck('models')->flatten()->unique('id'); + + // return $this->models->where(, AssetModel::class); } /** diff --git a/resources/views/models/custom_fields_form_bulk_edit.blade.php b/resources/views/models/custom_fields_form_bulk_edit.blade.php index c0c26db1cc..c932c0be41 100644 --- a/resources/views/models/custom_fields_form_bulk_edit.blade.php +++ b/resources/views/models/custom_fields_form_bulk_edit.blade.php @@ -7,17 +7,17 @@ @endphp @foreach($models as $model) @if (($model) && ($model->fieldset)) - @foreach($model->customFields AS $field) + @foreach($model->fieldset->fields AS $field) -@php - if (in_array($field->db_column_name(), $fields)) { - $duplicate = true; - continue; - } else { - $duplicate = false; - } - $fields[] = $field->db_column_name(); -@endphp + @php + if (in_array($field->db_column_name(), $fields)) { + $duplicate = true; + continue; + } else { + $duplicate = false; + } + $fields[] = $field->db_column_name(); + @endphp