Laravel删除产品-CRUD之delete(destroy)

  上一篇讲了Laravel编辑产品-CRUD之edit和update,现在我们讲一下删除产品,方法和前面的几篇文章类似,照着ytkah来操作吧

  1,controller的function destroy定义,注意这里的Name是destroy(controller的function查看方法在这),文件在/app/Http/Controllers/ItemController.php

1
2
3
4
5
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模板修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@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模板修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@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

  

 

posted @   ytkah  阅读(502)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2014-07-10 帝国cms调用随机文章 支持一个id段内的调用
2014-07-10 修改dedecms面包屑导航的首页链接关键字
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除

  
点击右上角即可分享
微信分享提示