TimiAde's avatar

Livewire Hook on Array property

How can i set this

public $foo = [" "," "," "," "," "," "," "," "," "," "];

// the updated* method follows your variable name and is camel-cased, in this sample, 'foo'
public function updatedFoo()
{
    // your code you want to execute
}
0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

So in your view you will have to wire that property to a model:

@foreach($foo as $i => $val)
<input type="text" wire:model="foo.{{$i}}">
@endforeach

now whenever you type anything in one of those input fields you will get the value and you can also get the index of the element in the array:

public function updatedFoo($newVal, $key)
{
	// this is just for debugging, you ofcourse would want to do something with the value.
	dd($newVal, $key);
}
1 like
Nakov's avatar

@Abin you can try. I haven't used that.

public function updatedFoo($newVal, $key)
{
	// set the value first
	
	$this->validate(); // this should handle validation if you have $rules and display the error if any	
}
1 like
Abin's avatar

@Nakov I am very new so I dont actually understand it.

public function updatingTitles($name, $value){ if( Str::length($name) > 0){ DB::table($this->table_name) ->where('id', (int)$value) ->update(['title' => $name,]); }else { $name = $this->titles[$value]; } $this->titles[$value] = $name; }

This prevents the empty data being saved in the table but the field retains the empty value and no error message.

Please or to participate in this conversation.