How to run `updated()` to all of the properties without writing like `updatedFooBar` to each property?

What seems to be the problem:

I have a lot of properties in my Livewire component, because I have also a lot of fields in my Model. I want that in every properties that has been updated() it automatically save it on model. It’s a “save as draft” feature.

The only idea that I have is these:

  1. Use updated() and save all the fields .e.g.:
public function updated()
 {
        $this->app->$first_name = $this->first_name;
        $this->app->$last_name = $this->last_name;
        .... etc (+20 fields)
        $this->app->save();
}

But the problem of this is that I don’t want to run a whole Eloquent function to just 1 property only.

  1. Use the updatedFooBar(). But it’s not practical since there would be a lot of fields. Imagine, creating 20 of this.
public function updatedFirstName()
    {
        $this->app->first_name = $this->first_name;
        $this->app->save();
    }

Is there anyway I can achieve something like:

public function updated($property){
$this->app->$property = $this->$propery;
}

Hey, @vahnmarty
I don’t think this is a good idea to hit the database every time the user type something, imagine that there are 1000 users typing at the same time. Your server can’t handle that much of queries every time -at minimum requirement-.

So, I suggest adding another column to your model called status

$this->enum('status', ['published', 'draft'])->default('draft');

After that when a user reloads the page for example, or he wants to go back, then you can make a function communicating with the DOM to listen to this type of events to fire a modal for example ask the user if he/she wants to save it to the draft or cancel it entirely.

So for detecting if the user reload the page:

if (window.performance) {
  alert('do you want to save your changes as draft?');

  // if yes Livewire.call('saveToDraft');

  // if not ... do nothing.
}

In Your component:

public function saveToDraft()
{
   // do not set the status here because in your migration you set it as draft by default
}
public function save()
{
// set the status to published
}

Maybe in your model you want to retrieve the published/drafted collections.

public function scopePublished()
{
    return $this->whereStatus('published');
}

public function scopeDrafted()
{
  return $this->whereStatus('draft');
}

I hope you find something helpful and if you find a solution please let me know.