zoukankan      html  css  js  c++  java
  • Laravel删除产品-CRUD之delete(destroy)

      上一篇讲了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
    

      

  • 相关阅读:
    The bean 'xxx' could not be injected as a 'xxx'because it is a JDK dynamic proxy that implements
    JQuery对象选择详细说明
    C#将image中的显示的图片转换成二进制
    选择一个图片文件,并且用PictureBox表示在Form上
    Android推送方案分析(MQTT/XMPP/GCM)
    [转载]什么是 Design Hackathon?
    脚本启动MySql服务
    Mysql数据库插入数据乱码问题,解决方案!
    MySQL自动备份脚本
    读取Excel模板,写入数据,并别输出另存为Excel文件
  • 原文地址:https://www.cnblogs.com/ytkah/p/9292019.html
Copyright © 2011-2022 走看看