Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:思考:不用中间件方法, 如果完成同样功能?
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//get方法的第一个参数'/'代表网站的入口地址,第二个参数访问的是app/http/controllers/home下的indexCon控制器的index方法
Route::get('/', 'home\indexCon@index');
//get方法的第一个参数'/home/article'访问地址为backstage.com/goods,第二个参数访问的是app/http/controllers/home下的goodsCon控制器的index方法
Route::get('/goods','home\goodsCon@index');
//添加商品
Route::get('/goods/add','home\goodsCon@addGoods');
Route::post('goods/add/detail','home\goodsCon@addDetail');
//删除商品
Route::get('/goods/del/{id}','home\goodsCon@delGoods');
//更新商品
Route::get('/goods/update/{id}','home\goodsCon@updateGoods')->middleware('checkLogin');
Route::post('/goods/update/detail','home\goodsCon@updateDetail')->middleware('checkLogin');
<?php
namespace App\Http\Controllers\home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class goodsCon extends Controller
{
//
public function index()
{
return $this->showGoods();
}
//显示商品页面
public function showGoods()
{
// $res = DB::select('select * from tb_goods');
// return view('home/goods',['res'=>$res]);
$cateids = DB::table('tb_goodscate')->select()->get()->all();
$goods = DB::table('tb_goods')->orderby('id','asc')->get()->all();
return view('home/goods',['res'=>$goods,'cates'=>$cateids]);
}
//显示添加商品页面
public function addGoods(Request $request)
{
// $date = date('Y-m-d');
// $res = DB::insert("insert into `tb_goods` (`name`,`price`,`unit`,`sdate`) values ('黄桃','10','斤','$date') ");
// echo $res;
$res = DB::table('tb_goodscate')->select()->get()->all();
return view('home/addGoods',['cates'=>$res]);
}
//执行添加商品操作
public function addDetail()
{
$cateid = $_POST['gCate'];
$name = $_POST['gName'];
$price = $_POST['gPrice'];
$unit = $_POST['gUnit'];
$sdate = $_POST['gSdate'];
$insertId = DB::table('tb_goods')->insertGetId([
'cate_id'=>"$cateid",
'name'=>"$name",
'price'=>"$price",
'unit'=>"$unit",
'sdate'=>"$sdate"
]);
echo $insertId;
return redirect('/goods');
}
//删除商品
public function delGoods($id)
{
// $res = DB::delete('delete from tb_goods where id>22');
// echo $res;
$res = DB::table('tb_goods')->where('id',$id)->delete();
return redirect('/goods');
}
//显示更新商品页面
public function updateGoods($id)
{
$cateids = DB::table('tb_goodscate')->select()->get()->all();
$goods = DB::table('tb_goods')->where('id',$id)->select()->get()->first();
return view('home/updateGoods',['res'=>$goods,'cates'=>$cateids]);
// $res = DB::update('update tb_goods set name="西瓜" where id="22"');
// echo $res;
}
//执行更新商品操作
public function updateDetail()
{
$id = $_POST['gId'];
$cateid = $_POST['gCate'];
$name = $_POST['gName'];
$price = $_POST['gPrice'];
$unit = $_POST['gUnit'];
$sdate = $_POST['gSdate'];
$res = DB::table('tb_goods')->where('id',"$id")->update([
'cate_id'=>"$cateid",
'name'=>"$name",
'price'=>"$price",
'unit'=>"$unit",
'sdate'=>"$sdate"
]);
return redirect('/goods');
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>前台商品展示页</title>
<link rel="stylesheet" href="layui/css/layui.css">
<script src="layui/layui.js"></script>
</head>
<style>
body>div:first-of-type {
width: 100%;
margin: 30px 0px;
}
body>div:first-of-type>h2 {
width: 120px;
letter-spacing: 1px;
margin-left: auto;
margin-right: auto;
}
</style>
<body>
<div>
<h2>商品展示页</h2>
</div>
<div>
<button class="layui-btn addBtn">添加商品</button>
</div>
<table class="layui-table">
<thead>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品类别</td>
<td>商品单价</td>
<td>商品单位</td>
<td>发布日期</td>
<td>操作</td>
</tr>
</thead>
<tbody>
@foreach($res as $goods)
<tr>
<td><?php echo $goods->id;?></td>
<td><?php echo $goods->name;?></td>
@foreach($cates as $cate)
@if($goods->cate_id===$cate->id)
<td>{{$cate->name}}</td>
@break;
@endif
@endforeach
<td><?php echo $goods->price;?></td>
<td><?php echo $goods->unit;?></td>
<td><?php echo $goods->sdate;?></td>
<td>
<button data-index=<?php echo $goods->id;?> class="layui-btn updateBtn">修改</button>
<button data-index=<?php echo $goods->id;?> class="layui-btn delBtn">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
<script>
//因为layui中的layer依赖jquery
layui.use(['layer'], function() {
var $ = layui.jquery;
$('.addBtn').click(function() {
window.location.href = '/goods/add';
});
$('.updateBtn').click(function(ev) {
//当前是第几条记录的更新操作
var uId = $(ev.target).attr('data-index');
window.location.href = '/goods/update/' + uId;
});
$('.delBtn').click(function(ev) {
var dId = $(ev.target).attr('data-index');
if (confirm('确认删除该记录?')) {
window.location.href = '/goods/del/' + dId;
}
})
});
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../../layui/css/layui.css">
<script src="../../layui/layui.js"></script>
<title>添加商品</title>
</head>
<style>
.main {
width: 100%;
height: 400px;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-items: center;
}
</style>
<body>
<div class="main">
<div>
<h2>添加商品</h2>
</div>
<form class="layui-form" method="POST" action="add/detail">
<!-- 如果使用post方式提交 ,需要添加@csrf -->
@csrf
<div class="layui-form-item">
<label class="layui-form-label">商品类别:</label>
<div class="layui-input-inline">
<select name="gCate" id="gCate">
<option value=""></option>
<!-- 通过类别id,显示类别名称 -->
@foreach($cates as $cate )
<option value="{{$cate->id}}">{{$cate->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">商品名称:</label>
<div class="layui-input-inline">
<input type="text" name="gName" id="gName" class="layui-input">
</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">商品单价:</label>
<div class="layui-input-inline">
<input type="text" name="gPrice" id="gPrice" class="layui-input">
</div>
<div class="layui-form-mid layui-word-aux">元</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">商品单位:</label>
<div class="layui-input-inline">
<input type="text" name="gUnit" id="gUnit" class="layui-input">
</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">上架时间:</label>
<div class="layui-input-inline">
<input type="text" name="gSdate" id="gSdate" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submnt id="addGoodBtn">添加</button>
<button type="reset" class="layui-btn layui-btn-primary" id="backBtn">重置</button>
</div>
</div>
<div></div>
</form>
</div>
</body>
<script>
layui.use(['form', 'laydate'], function() {
var form = layui.form;
var laydate = layui.laydate;
laydate.render({
elem: '#gSdate'
});
});
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../../layui/css/layui.css">
<script src="../../layui/layui.js"></script>
<title>修改商品</title>
<style>
.main {
width: 100%;
height: 470px;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-items: center;
}
</style>
</head>
<body>
<div class="main">
<div>
<h2 style="margin-bottom: 20px;">更新商品</h2>
</div>
<form class="layui-form" method="POST" action="/goods/update/detail">
<!-- 如果使用post方式提交 ,需要添加@csrf -->
@csrf
<div class="layui-form-item">
<label class="layui-form-label">商品编号:</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" value="{{$res->id}}" disabled>
<input type="hidden" name="gId" id="gId" value="{{$res->id}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">商品类别:</label>
<div class="layui-input-inline">
<select name="gCate" id="gCate">
@foreach($cates as $cate )
@if($cate->id===$res->cate_id)
<option value="{{$cate->id}}" selected>{{$cate->name}}</option>
@else
<option value="{{$cate->id}}">{{$cate->name}}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">商品名称:</label>
<div class="layui-input-inline">
<input type="text" name="gName" id="gName" class="layui-input" value="{{$res->name}}">
</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">商品单价:</label>
<div class="layui-input-inline">
<input type="text" name="gPrice" id="gPrice" class="layui-input" value="{{$res->price}}">
</div>
<div class="layui-form-mid layui-word-aux">元</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">商品单位:</label>
<div class="layui-input-inline">
<input type="text" name="gUnit" id="gUnit" class="layui-input" value="{{$res->unit}}">
</div>
</div>
<div class=" layui-form-item">
<label class="layui-form-label">上架时间:</label>
<div class="layui-input-inline">
<input type="text" name="gSdate" id="gSdate" class="layui-input" value="{{$res->sdate}}">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submnt id="addGoodBtn">更新</button>
<button type="reset" class="layui-btn layui-btn-primary" id="backBtn">重置</button>
</div>
</div>
<div></div>
</form>
</div>
</body>
<script>
layui.use(['form', 'laydate'], function() {
var form = layui.form;
var laydate = layui.laydate;
laydate.render({
elem: '#gSdate'
});
});
</script>
</html>
创建中间件
注册中间件
使用中间件
使用中间件页面效果图