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!
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