Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #302
    jacob_smith
    Participant

    How do you update a model from a Livewire Class Component?

    class UserUpdate extends Component
    {
    public $name;

    public function render()
    {
    $this->name = Auth::user()->name;

    return view(‘livewire.user-update’);
    }

    public function submitForm()
    {
    Auth::user()->name = $this->name;
    }
    }

    #303

    The way you update a model from a Livewire Component is the same way how you update a model in Laravel.

    For example in the submitForm() function in your Livewire component you can write the following:

    public function submitForm()
    {
    Auth::user()->update([‘name’ => $this->name]);
    }

    You could also the same method
    https://laravel.com/docs/10.x/eloquent#updates

    but its a little more verbose and you have to import your User class

    $user = User::where(‘name’, Auth::user()->name);

    $user->name = $this -> name;

    $user->save();

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.