Livewire: How to use route model binding properly with Livewire?

Created on 28 Jul 2019  路  4Comments  路  Source: livewire/livewire

Sorry if the answer to this is super obvious, I am newish to Laravel and obviously new to Livewire! I am just looking for some general advice.

Is the below how you would expect to create a simple method to update a record in the database? Am I supposed to use mount with route model binding in this way? I.e. inject the model instance, get the id, assign the id to a public property and then use this id within the update function to get the model instance again?

namespace App\Http\Livewire;

use Livewire\Component;
use App\Project;

class ProjectsEdit extends Component
{
    public $id;
    public $title;

    public function mount(Project $project)
    {
        $this->id = $project->id;
        $this->title = $project->title;
    }

    public function update()
    {
        $attributes = $this->validate([
            'title' => 'required|min:10',
        ]);

        Project::find($this->id)->update($attributes);

        $this->redirect('/home');
    }

    public function render()
    {
        return view('livewire.projects-edit');
    }
}

I am loving Livewire, amazing work by Caleb!

All 4 comments

https://github.com/rverrips/livewire-todomvc Is a sample project in Livewire which saves to database.
Perhaps that鈥檒l help you ?

Yes, that looks like correct usage. Technically you can store an array of models in the component.

Great, thanks for the link and the general advice. I have to say I am blown away by Livewire so far. I think the potential of this package to reduce overall complexity is massive.

Broken link

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tanthammar picture tanthammar  路  3Comments

tanthammar picture tanthammar  路  3Comments

zaherg picture zaherg  路  4Comments

austenc picture austenc  路  3Comments

roni-estein picture roni-estein  路  3Comments