Dynamic wire:model=“$something”

I want to have an arbitrary string in $something and pass that over to wire:model but I’m not being able to do so. Is this even possible?

Without any code everything is possible…

Hi,

Can you not do the following:

public $something = ‘Some text’;

That would put ‘Some Text’ in your wire:model

That’s not exactly what I need. Take this example:

// team.blade
<div>
    <!-- some UI here -->
    <input wire:model="$dynamicModel">
</div>

In the parent view…

<x-team :dynamic-model='foo'>
<x-team :dynamic-model='bar'>

This should fix your issue:

<input wire:model="dynamicModel">

Hi @Dimonka2 thanks for the prompt response.

I tried your suggestion but I’m getting the following error:

Livewire\Exceptions\PropertyNotFoundException

Property [$dynamicModel] not found on component: [match-prediction-form]

The odd thing is that if I output that variable anywhere in the template like this {{$dynamicModel}} it shows the value I expect only LiveWire doesn’t seem to be able to detect it.


And of course as I was spell checking before submitting this message I realized how to do this. It was a simple as doing this:

<input wire:model="{{$dynamicModel}}">

:man_facepalming:

1 Like

Maybe it’s better to paste some code

Now I got your problem. And I hope it is resolved.

exactly

every public variable of your component can be accessed in the view in this way

every public variable is a component property

and if you want to initialize the variable with some value just assign the value to the component

in the mount method

public $dynamicModel;

public function mount() {
    $this-> dynamicModel = 'value';
}

or directly in the variable declaration (without the mount method)
public $dynamicModel = 'value';

Добрый день, Дмитрий! Может подскажете, что делать, если переменные в представлении формируются динамически, то есть:

  • у меня есть структура базы, в которой пользователь создаёт таблицы, при этом имена таблиц формируются динамически, имена полей в таблицах формируются также динамически;
  • при необходимости добавления данных в базу формируется представление с формой и инпутами (select, text, date), имена инпутов принимаются в соответствии с именами полей таблицы.
    При наличии поля с моделью: wire:model="param{{ $param->id }}" появляется ошибка:
    Property [$param28] not found on component: [doc.dog.parametrs]
    Да, в компоненте нет такого публичного свойства, но как его там создать? Присваивать также динамически? возможно ли это?

Добрый день. Если очень-очень хочется динамических имён, то я бы не привязывался бы к переменным класса, а делал бы маппинг внутри массива:

public $dynamicMapping = [];
public $inputID;

И затем в блейде используй для привязке к любому индексу массива:

  <input wire:model="dynamicMapping.{{$inputID}}">

Так не нужно создавать никаких дополнительных публичных свойств.

1 Like