0
votes

I have a "custom_fields "table where I store fields that a user has created. I need to use the "field_slug" value from that table to form the properties in another form. By default the public properties do not exist so an error("Property [$field_name] not found on component") is thrown when an attempt to enter data inside the input field takes place.

I know that livewire utilizes public properties, but because the data coming back is dynamic I am not able to(or know how to) create those public properties for use within the form. So, the "field_name" within the array is what I would want to act as a public property(or this may be an incorrect approach, not sure) so I can store the values entered within the input by a user.

Any useful assistance with this problem would be greatly appreciated.


Data within CustomField

enter image description here


Component enter image description here


HTML(blade) enter image description here

1
According to your code, it is expecting public $field_name in your component.Digvijay

1 Answers

0
votes

My solution was to call my custom fields query in livewire's mount() method and assign the values to a custom field property. I mutated the values assigned in the $this->customFields property and assigned it to a $this->formSlugs property

public function mount()
{
    $this->customFields = CustomField::all();

    $data = [];
    $this->formSlugs = collect($this->customFields)->map(function($value) use ($data) {
        $data[$value->field_slug] = '';
        return $data;
    })->toArray();
}


public function render()
{
    return view('livewire.inventory.items.create', ['customFields' => $this->customFields])
        ->extends('layouts.master')
        ->section('content');
}

In the HTML

@foreach($customFields as $key => $customField)
    <input type="text" wire:model="formSlugs.{{$key}}.{{$customField->field_slug}}" class="form-control focus:placeholder-transparent" placeholder="{{ $customField->placeholder_text }}">
@endforeach