Use computed fields in CustomFieldSetDefaultValuesForModel component

This commit is contained in:
Marcus Moore 2024-08-13 17:01:35 -07:00
parent 74e7b1cfa5
commit f6c1642bf2
No known key found for this signature in database
2 changed files with 17 additions and 23 deletions

View file

@ -2,6 +2,7 @@
namespace App\Livewire; namespace App\Livewire;
use Livewire\Attributes\Computed;
use Livewire\Component; use Livewire\Component;
use App\Models\CustomFieldset; use App\Models\CustomFieldset;
@ -15,30 +16,23 @@ class CustomFieldSetDefaultValuesForModel extends Component
public $fields; public $fields;
public $model_id; public $model_id;
public function mount() public function mount($model_id = null)
{ {
if(is_null($this->model_id)){ $this->model_id = $model_id;
return; $this->fieldset_id = $this->model?->fieldset_id;
} $this->add_default_values = ($this->model?->defaultValues->count() > 0);
$this->model = AssetModel::find($this->model_id); // It's possible to do some clever route-model binding here, but let's keep it simple, shall we?
$this->fieldset_id = $this->model->fieldset_id;
$this->fields = null;
if ($fieldset = CustomFieldset::find($this->fieldset_id)) {
$this->fields = CustomFieldset::find($this->fieldset_id)->fields;
}
$this->add_default_values = ($this->model->defaultValues->count() > 0);
} }
public function updatedFieldsetId() #[Computed]
public function model()
{ {
if (CustomFieldset::find($this->fieldset_id)) { return AssetModel::find($this->model_id);
$this->fields = CustomFieldset::find($this->fieldset_id)->fields; }
}
#[Computed]
public function customFields()
{
return CustomFieldset::find($this->fieldset_id)?->fields;
} }
public function render() public function render()

View file

@ -16,10 +16,10 @@
</div> </div>
</div> </div>
@if ($this->add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} @if ($add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}}
@if ($fields) @if ($this->customFields)
@foreach ($fields as $field) @foreach ($this->customFields as $field)
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label{{ $errors->has($field->name) ? ' has-error' : '' }}">{{ $field->name }}</label> <label class="col-md-3 control-label{{ $errors->has($field->name) ? ' has-error' : '' }}">{{ $field->name }}</label>