Membuat fitur Update

namespace App\Livewire\Forms;

use App\Models\Post; use Livewire\Attributes\Rule; use Livewire\Form;

class PostForm extends Form { public Post $post;

#[Rule('required|min:3')]
public $title = '';
 
#[Rule('required|min:3')]
public $body = '';
 
public function setPost(Post $post)
{
$this->post = $post;
 
$this->title = $post->title;
 
$this->body = $post->body;
}
 
public function store()
{
Post::create($this->all());
}
 
public function update()
{
$this->post->update($this->all());
}

}

namespace App\Livewire;

use App\Livewire\Forms\PostForm; use App\Models\Post; use Livewire\Attributes\Layout; use Livewire\Component;

class PostUpdate extends Component { public PostForm $form;

public function mount(Post $post)
{
$this->form->setPost($post);
}
 
public function save()
{
$this->validate();
$this->form->update();
return $this->redirect('/posts');
}
 
#[Layout('layouts.app')]
public function render()
{
return view('livewire.post-create');
}

}

Komentar

Ada 0 komentar pada episode ini.