上一篇讲了Laravel编辑产品-CRUD之edit和update,现在我们讲一下删除产品,方法和前面的几篇文章类似,照着ytkah来操作吧
1,controller的function destroy定义,注意这里的Name是destroy(controller的function查看方法在这),文件在/app/Http/Controllers/ItemController.php
public function destroy($id) { $item = Item::find($id); $item->delete(); }
2,模板的编辑,有两个地方,show.blade.php和index.blade.php,用<input type="hidden" name="_method" value="DELETE">的方法
show.blade.php模板修改
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <div class="card"> <div class="card-header">Item:{{$item->id}}</div> <div class="card-body"> <div class="col-md-8" style="float: left;"> <div class="form-group row"> <label class="col-md-2 text-md-right" >ID:</label> <div class="col-md-6">{{$item->id}}</div> </div> <div class="form-group row"> <label class="col-md-2 text-md-right">Name:</label> <div class="col-md-6">{{$item->name}}</div> </div> <div class="form-group row"> <label class="col-md-2 text-md-right">Price:</label> <div class="col-md-6">{{$item->price}}</div> </div> <div class="form-group row"> <label class="col-md-2 text-md-right">Description:</label> <div class="col-md-6">{{$item->description}}</div> </div> </div> <div class="col-md-4" style="float: left;"> <dl class="well"> <label>Created At:</label> <div>{{$item->created_at}}</div> <label>Updated At:</label> <div>{{$item->updated_at}}</div> </dl> <div class="row"> <div class="col-md-6"> <a class="btn btn-primary" href="{{route('items.edit', $item->id)}}">edit</a> </div> <div class="col-md-6"> <form method="POST" action="{{route('items.update', $item->id)}}" aria-label="Register"> @csrf <input type="hidden" name="_method" value="DELETE"> <input type="submit" class="btn btn-danger" value="DELETE"> </form> </div> </div> </div> </div> </div> </div> </div> </div> @endsection
index.blade.php模板修改
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <div class="card card-default"> <div class="card-header">List of Items</div> <div class="card-body"> <table class="table"> <thead> <tr> <th>#</th> <th>Name</th> <th>Price</th> <th>Img</th> <th>description</th> <th>Created At</th> <th>Update At</th> <th>Actions</th> </tr> </thead> <tbody> @foreach($items as $item) <tr> <td>{{$item->id}}</td> <td>{{$item->name}}</td> <td>{{$item->price}}</td> <td>{{$item->img}}</td> <td>{{$item->description}}</td> <td>{{$item->created_at}}</td> <td>{{$item->updated_at}}</td> <td> <a style="float: left;" class="btn btn-primary" href="{{route('items.show', $item->id)}}">view</a> <form style="float: left;" method="POST" action="{{route('items.update', $item->id)}}" aria-label="Register"> @csrf <input type="hidden" name="_method" value="DELETE"> <input type="submit" class="btn btn-danger" value="DELETE"> </form> </td> </tr> @endforeach </tbody> </table> <div class="text-center">{{$items->links()}}</div> <a class="btn btn-primary" href="{{route('items.create')}}">Create New Item</a> </div> </div> </div> </div> </div> @endsection