diff --git a/app/Livewire/TodoEdit.php b/app/Livewire/TodoEdit.php index bd996b0..42e0960 100644 --- a/app/Livewire/TodoEdit.php +++ b/app/Livewire/TodoEdit.php @@ -6,10 +6,37 @@ class TodoEdit extends Component { - public $counter = 0; + // public $counter = 0; + public $is_editing = false; + public $todo; + public $task; + // ★コンポーネントの初期化★ + public function mount($todo) + { + $this->todo = $todo; + $this->task = $todo->task; + } + + // ★コンポーネントの描画★ public function render() { return view('livewire.todo-edit'); } + + // ★★★ wire:model.liveから更新される関数の名前は、updated + プロパティ名(先頭大文字)にしなければならない★★★ + public function updatedTask() + { + // info('updateTask called'); // for debug + $this->todo->task = $this->task; + $this->todo->save(); + } + + // ここは、自由な関数名で良い + public function saveMyData() + { + $this->updatedTask(); // 保存する + $this->is_editing = false; + } + } diff --git a/resources/views/livewire/todo-edit.blade.php b/resources/views/livewire/todo-edit.blade.php index c791010..9a6dfd6 100644 --- a/resources/views/livewire/todo-edit.blade.php +++ b/resources/views/livewire/todo-edit.blade.php @@ -1,5 +1,11 @@ -
- 値は: {{ $counter }} です。 - -
- + + @if ($is_editing) + + @else + {{ $todo->task }} + + @endif + {{-- 値は: {{ $counter }} です。 + --}} + diff --git a/resources/views/todo/index.blade.php b/resources/views/todo/index.blade.php index af3b7a4..2fc9a2a 100644 --- a/resources/views/todo/index.blade.php +++ b/resources/views/todo/index.blade.php @@ -8,8 +8,6 @@ @endif
- -
@@ -26,7 +24,7 @@ @foreach ($todos as $todo) - +
{{ $todo->id }}{{ $todo->task }}{{ $todo->created_at }} {{ $todo->updated_at }}