How to dynamically change form values

I have 2 models:

class CreateHomesTable extends Migration {
    public function up()    {
        Schema::create('homes', function (Blueprint $table) {
            $table->string('prefix', 5);
        });
    }

class CreateConsumersTable extends Migration {
    public function up()    {
        Schema::create('consumers', function (Blueprint $table) {
            $table->foreignId('home_id')->constrained();
            $table->string('ls', 20);
        });
    }

When creating new Consumer opens a hidden modal form and I shoul select home and vould like the LS input value of form will dynamicaly change its value to prefix acording to the selecrted home. How do it?2020-12-24_17-19-22

Hey, @RizONEnew

I don’t get your question properly, but you can do it with two options

The first one:

use updated property, for example you have $home property and $ls property

...
public $home;
public $ls;

public function updateHome()
{
   // change the value of $this->ls here after the home property was updated
  $this->ls = ...
} 

Second Method

update the $ls method in render method

public function render()
{
   if (!empty($this->home)) {
      $this->ls = ...
   }

Since we Don’t know what’s the type of $home you are free to chose what ever you want.

I have 2 records in my home db table
name = “Test home 44”, prefix=“44”
name = “Test home 45”, prefix=“45”

On Consumer add form I have home selector and LS text field
When I have choose “Test home 44” in home field, LS field automatically should = “44”
When I have choose “Test home 45” in home field, LS field automatically should = “45”

public $home;
public function updatedHome($value)
{
  $ls = str_replace('Test home', ' ' , $value);
  $this->ls = $ls;
}

Thanx a lot!
Did it by this way:

public function updated($value) {
        if ($value == "home_id")
            $this->ls = DB::table('homes')
            ->select('homes.prefix')
            ->where('homes.id', '=', $this->home_id)
            ->value('homes.prefix');
    }

Does it good approach?

PS
I didnt find something like

public function updatedHome_id($value) 
public function updatedName($value)

Use Camel Case for this updatedHomeId($value).

PS: If you just need the value use your code above, but you must know in every request you made in this case, you are hitting the database in every request and this is not good for performance. So, keep it simple and clean and just update the $ls property and after that you can save all of it later.