*刪除資料
1.view
resources\views\todo\index.blade.php
加入刪除按鈕:
<form action="{{url('todo/'.$todo->id)}}" method="POST">
@csrf
@method('DELETE')
<input type="submit" value="刪除">
</form>
位置如下:
@foreach ($todos as $todo)
<p>
{{ $todo->id.'.'.$todo->title }}
<form action="{{url('todo/'.$todo->id)}}" method="POST">
@csrf
@method('DELETE')
<input type="submit" value="刪除">
</form>
</p>
@endforeach
<form action="{{url('todo')}}" method="POST">
@csrf
<input type="text" placeholder="請輸入" name="title">
<input type="submit" value="送出">
</form>
2.route
routes\web.php
Route::delete('/todo/{id}', 'TodoController@delete');
3.controller
app\Http\Controllers\TodoController.php
use App\Todo; //model
//方法1
public function delete($id)
{
Todo::destroy($id);
return redirect('todo');
}
//方法2
public function delete($id)
{
$todo=Game::find($id);
$todo->delete();
return redirect('todo');
}
//方法3
public function delete(Todo $id)
{
//測試
// dd($id);
// return $id;
$id->delete();
return redirect('todo');
}
沒有留言:
張貼留言