Blogger Information
Blog 64
fans 6
comment 2
visits 83040
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel 使用数据库查询构造器实现数据的增删改查,页面的渲染,中间件的使用
王娇
Original
934 people have browsed it

学习总结

  • 使用laravel中的查询构造器,也就是链式调用,实现数据库中数据的增删改查,方便且高效
  • 中间件的注册和使用,可以很方便的解决用户操作的控制

1.路由 web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Web Routes
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here is where you can register web routes for your application. These
  9. | routes are loaded by the RouteServiceProvider within a group which
  10. | contains the "web" middleware group. Now create something great!
  11. |
  12. */
  13. //get方法的第一个参数'/'代表网站的入口地址,第二个参数访问的是app/http/controllers/home下的indexCon控制器的index方法
  14. Route::get('/', 'home\indexCon@index');
  15. //get方法的第一个参数'/home/article'访问地址为backstage.com/goods,第二个参数访问的是app/http/controllers/home下的goodsCon控制器的index方法
  16. Route::get('/goods','home\goodsCon@index');
  17. //添加商品
  18. Route::get('/goods/add','home\goodsCon@addGoods');
  19. Route::post('goods/add/detail','home\goodsCon@addDetail');
  20. //删除商品
  21. Route::get('/goods/del/{id}','home\goodsCon@delGoods');
  22. //更新商品
  23. Route::get('/goods/update/{id}','home\goodsCon@updateGoods')->middleware('checkLogin');
  24. Route::post('/goods/update/detail','home\goodsCon@updateDetail')->middleware('checkLogin');

2.goodsCon控制器

  1. <?php
  2. namespace App\Http\Controllers\home;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use DB;
  6. class goodsCon extends Controller
  7. {
  8. //
  9. public function index()
  10. {
  11. return $this->showGoods();
  12. }
  13. //显示商品页面
  14. public function showGoods()
  15. {
  16. // $res = DB::select('select * from tb_goods');
  17. // return view('home/goods',['res'=>$res]);
  18. $cateids = DB::table('tb_goodscate')->select()->get()->all();
  19. $goods = DB::table('tb_goods')->orderby('id','asc')->get()->all();
  20. return view('home/goods',['res'=>$goods,'cates'=>$cateids]);
  21. }
  22. //显示添加商品页面
  23. public function addGoods(Request $request)
  24. {
  25. // $date = date('Y-m-d');
  26. // $res = DB::insert("insert into `tb_goods` (`name`,`price`,`unit`,`sdate`) values ('黄桃','10','斤','$date') ");
  27. // echo $res;
  28. $res = DB::table('tb_goodscate')->select()->get()->all();
  29. return view('home/addGoods',['cates'=>$res]);
  30. }
  31. //执行添加商品操作
  32. public function addDetail()
  33. {
  34. $cateid = $_POST['gCate'];
  35. $name = $_POST['gName'];
  36. $price = $_POST['gPrice'];
  37. $unit = $_POST['gUnit'];
  38. $sdate = $_POST['gSdate'];
  39. $insertId = DB::table('tb_goods')->insertGetId([
  40. 'cate_id'=>"$cateid",
  41. 'name'=>"$name",
  42. 'price'=>"$price",
  43. 'unit'=>"$unit",
  44. 'sdate'=>"$sdate"
  45. ]);
  46. echo $insertId;
  47. return redirect('/goods');
  48. }
  49. //删除商品
  50. public function delGoods($id)
  51. {
  52. // $res = DB::delete('delete from tb_goods where id>22');
  53. // echo $res;
  54. $res = DB::table('tb_goods')->where('id',$id)->delete();
  55. return redirect('/goods');
  56. }
  57. //显示更新商品页面
  58. public function updateGoods($id)
  59. {
  60. $cateids = DB::table('tb_goodscate')->select()->get()->all();
  61. $goods = DB::table('tb_goods')->where('id',$id)->select()->get()->first();
  62. return view('home/updateGoods',['res'=>$goods,'cates'=>$cateids]);
  63. // $res = DB::update('update tb_goods set name="西瓜" where id="22"');
  64. // echo $res;
  65. }
  66. //执行更新商品操作
  67. public function updateDetail()
  68. {
  69. $id = $_POST['gId'];
  70. $cateid = $_POST['gCate'];
  71. $name = $_POST['gName'];
  72. $price = $_POST['gPrice'];
  73. $unit = $_POST['gUnit'];
  74. $sdate = $_POST['gSdate'];
  75. $res = DB::table('tb_goods')->where('id',"$id")->update([
  76. 'cate_id'=>"$cateid",
  77. 'name'=>"$name",
  78. 'price'=>"$price",
  79. 'unit'=>"$unit",
  80. 'sdate'=>"$sdate"
  81. ]);
  82. return redirect('/goods');
  83. }
  84. }

3.前端页面

3.1商品显示页面goods.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>前台商品展示页</title>
  7. <link rel="stylesheet" href="layui/css/layui.css">
  8. <script src="layui/layui.js"></script>
  9. </head>
  10. <style>
  11. body>div:first-of-type {
  12. width: 100%;
  13. margin: 30px 0px;
  14. }
  15. body>div:first-of-type>h2 {
  16. width: 120px;
  17. letter-spacing: 1px;
  18. margin-left: auto;
  19. margin-right: auto;
  20. }
  21. </style>
  22. <body>
  23. <div>
  24. <h2>商品展示页</h2>
  25. </div>
  26. <div>
  27. <button class="layui-btn addBtn">添加商品</button>
  28. </div>
  29. <table class="layui-table">
  30. <thead>
  31. <tr>
  32. <td>商品编号</td>
  33. <td>商品名称</td>
  34. <td>商品类别</td>
  35. <td>商品单价</td>
  36. <td>商品单位</td>
  37. <td>发布日期</td>
  38. <td>操作</td>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. @foreach($res as $goods)
  43. <tr>
  44. <td><?php echo $goods->id;?></td>
  45. <td><?php echo $goods->name;?></td>
  46. @foreach($cates as $cate)
  47. @if($goods->cate_id===$cate->id)
  48. <td>{{$cate->name}}</td>
  49. @break;
  50. @endif
  51. @endforeach
  52. <td><?php echo $goods->price;?></td>
  53. <td><?php echo $goods->unit;?></td>
  54. <td><?php echo $goods->sdate;?></td>
  55. <td>
  56. <button data-index=<?php echo $goods->id;?> class="layui-btn updateBtn">修改</button>
  57. <button data-index=<?php echo $goods->id;?> class="layui-btn delBtn">删除</button>
  58. </td>
  59. </tr>
  60. @endforeach
  61. </tbody>
  62. </table>
  63. </body>
  64. <script>
  65. //因为layui中的layer依赖jquery
  66. layui.use(['layer'], function() {
  67. var $ = layui.jquery;
  68. $('.addBtn').click(function() {
  69. window.location.href = '/goods/add';
  70. });
  71. $('.updateBtn').click(function(ev) {
  72. //当前是第几条记录的更新操作
  73. var uId = $(ev.target).attr('data-index');
  74. window.location.href = '/goods/update/' + uId;
  75. });
  76. $('.delBtn').click(function(ev) {
  77. var dId = $(ev.target).attr('data-index');
  78. if (confirm('确认删除该记录?')) {
  79. window.location.href = '/goods/del/' + dId;
  80. }
  81. })
  82. });
  83. </script>
  84. </html>
  • 页面效果

3.2添加商品页面addGoods.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="../../layui/css/layui.css">
  7. <script src="../../layui/layui.js"></script>
  8. <title>添加商品</title>
  9. </head>
  10. <style>
  11. .main {
  12. width: 100%;
  13. height: 400px;
  14. display: flex;
  15. flex-flow: column nowrap;
  16. justify-content: space-evenly;
  17. align-items: center;
  18. }
  19. </style>
  20. <body>
  21. <div class="main">
  22. <div>
  23. <h2>添加商品</h2>
  24. </div>
  25. <form class="layui-form" method="POST" action="add/detail">
  26. <!-- 如果使用post方式提交 ,需要添加@csrf -->
  27. @csrf
  28. <div class="layui-form-item">
  29. <label class="layui-form-label">商品类别:</label>
  30. <div class="layui-input-inline">
  31. <select name="gCate" id="gCate">
  32. <option value=""></option>
  33. <!-- 通过类别id,显示类别名称 -->
  34. @foreach($cates as $cate )
  35. <option value="{{$cate->id}}">{{$cate->name}}</option>
  36. @endforeach
  37. </select>
  38. </div>
  39. </div>
  40. <div class="layui-form-item">
  41. <label class="layui-form-label">商品名称:</label>
  42. <div class="layui-input-inline">
  43. <input type="text" name="gName" id="gName" class="layui-input">
  44. </div>
  45. </div>
  46. <div class=" layui-form-item">
  47. <label class="layui-form-label">商品单价:</label>
  48. <div class="layui-input-inline">
  49. <input type="text" name="gPrice" id="gPrice" class="layui-input">
  50. </div>
  51. <div class="layui-form-mid layui-word-aux"></div>
  52. </div>
  53. <div class=" layui-form-item">
  54. <label class="layui-form-label">商品单位:</label>
  55. <div class="layui-input-inline">
  56. <input type="text" name="gUnit" id="gUnit" class="layui-input">
  57. </div>
  58. </div>
  59. <div class=" layui-form-item">
  60. <label class="layui-form-label">上架时间:</label>
  61. <div class="layui-input-inline">
  62. <input type="text" name="gSdate" id="gSdate" class="layui-input">
  63. </div>
  64. </div>
  65. <div class="layui-form-item">
  66. <div class="layui-input-block">
  67. <button class="layui-btn" lay-submnt id="addGoodBtn">添加</button>
  68. <button type="reset" class="layui-btn layui-btn-primary" id="backBtn">重置</button>
  69. </div>
  70. </div>
  71. <div></div>
  72. </form>
  73. </div>
  74. </body>
  75. <script>
  76. layui.use(['form', 'laydate'], function() {
  77. var form = layui.form;
  78. var laydate = layui.laydate;
  79. laydate.render({
  80. elem: '#gSdate'
  81. });
  82. });
  83. </script>
  84. </html>
  • 页面效果

3.3更新商品页面updateGoods.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="../../layui/css/layui.css">
  7. <script src="../../layui/layui.js"></script>
  8. <title>修改商品</title>
  9. <style>
  10. .main {
  11. width: 100%;
  12. height: 470px;
  13. display: flex;
  14. flex-flow: column nowrap;
  15. justify-content: space-evenly;
  16. align-items: center;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="main">
  22. <div>
  23. <h2 style="margin-bottom: 20px;">更新商品</h2>
  24. </div>
  25. <form class="layui-form" method="POST" action="/goods/update/detail">
  26. <!-- 如果使用post方式提交 ,需要添加@csrf -->
  27. @csrf
  28. <div class="layui-form-item">
  29. <label class="layui-form-label">商品编号:</label>
  30. <div class="layui-input-inline">
  31. <input type="text" class="layui-input" value="{{$res->id}}" disabled>
  32. <input type="hidden" name="gId" id="gId" value="{{$res->id}}">
  33. </div>
  34. </div>
  35. <div class="layui-form-item">
  36. <label class="layui-form-label">商品类别:</label>
  37. <div class="layui-input-inline">
  38. <select name="gCate" id="gCate">
  39. @foreach($cates as $cate )
  40. @if($cate->id===$res->cate_id)
  41. <option value="{{$cate->id}}" selected>{{$cate->name}}</option>
  42. @else
  43. <option value="{{$cate->id}}">{{$cate->name}}</option>
  44. @endif
  45. @endforeach
  46. </select>
  47. </div>
  48. </div>
  49. <div class="layui-form-item">
  50. <label class="layui-form-label">商品名称:</label>
  51. <div class="layui-input-inline">
  52. <input type="text" name="gName" id="gName" class="layui-input" value="{{$res->name}}">
  53. </div>
  54. </div>
  55. <div class=" layui-form-item">
  56. <label class="layui-form-label">商品单价:</label>
  57. <div class="layui-input-inline">
  58. <input type="text" name="gPrice" id="gPrice" class="layui-input" value="{{$res->price}}">
  59. </div>
  60. <div class="layui-form-mid layui-word-aux"></div>
  61. </div>
  62. <div class=" layui-form-item">
  63. <label class="layui-form-label">商品单位:</label>
  64. <div class="layui-input-inline">
  65. <input type="text" name="gUnit" id="gUnit" class="layui-input" value="{{$res->unit}}">
  66. </div>
  67. </div>
  68. <div class=" layui-form-item">
  69. <label class="layui-form-label">上架时间:</label>
  70. <div class="layui-input-inline">
  71. <input type="text" name="gSdate" id="gSdate" class="layui-input" value="{{$res->sdate}}">
  72. </div>
  73. </div>
  74. <div class="layui-form-item">
  75. <div class="layui-input-block">
  76. <button class="layui-btn" lay-submnt id="addGoodBtn">更新</button>
  77. <button type="reset" class="layui-btn layui-btn-primary" id="backBtn">重置</button>
  78. </div>
  79. </div>
  80. <div></div>
  81. </form>
  82. </div>
  83. </body>
  84. <script>
  85. layui.use(['form', 'laydate'], function() {
  86. var form = layui.form;
  87. var laydate = layui.laydate;
  88. laydate.render({
  89. elem: '#gSdate'
  90. });
  91. });
  92. </script>
  93. </html>
  • 页面效果

4.中间件的使用

  • 创建中间件

  • 注册中间件

  • 使用中间件

  • 使用中间件页面效果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:思考:不用中间件方法, 如果完成同样功能?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post