Skip to content

Model binding

tanthammar edited this page Nov 18, 2020 · 6 revisions

The form component expects an Eloquent Model instance via the mount_form($model) method.


It can either be a new Model instance or an existing one.

public function mount(?SomeEloquentModel $model) {
    //Laravel automatically creates a new instance, if none is passed in the route
    $this->mount_form($model);
}

Various ways to pass the model to the component

In a view

blade

<livewire:some-component :model="$some_model" />

component

public function mount($model) {
    $this->mount_form($model);
}

Route model binding

Using Livewire route, full-page components web.php

Route::get('/user/{user}', UserForm::class);

component

public function mount(User $user) {
    $this->mount_form($user);
}

Optional model binding

Using Livewire route, full-page components web.php

Route::get('/user/{user?}', UserForm::class);

component

public function mount(?User $user) {
    //Laravel automatically creates a new User if none is passed in the route
    $this->mount_form($user);
}
Clone this wiki locally