博主信息
博文 38
粉丝 1
评论 0
访问量 36733
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
12月30日_blade模板引擎和数据库基本操作
fkkf467
原创
947人浏览过

一、blade模板引擎

控制器:Officer.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. class Officer extends Controller
  5. {
  6. public function find()
  7. {
  8. $data = DB::table('staffs')->get()->toArray();
  9. return view('index.people',['people'=>$data]);
  10. }
  11. }

路由

  1. Route::get('/index/show','Officer@find');

1. foreach

视图:people.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>查看政府人员</title>
  6. </head>
  7. <body>
  8. <table align="center">
  9. <caption>政府人员信息</caption>
  10. <thead>
  11. <tr>
  12. <th>姓名</th>
  13. <th>年龄</th>
  14. <th>性别</th>
  15. <th>职位</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. @foreach($people as $value)
  20. <tr>
  21. <th>{{$value->name}}</th>
  22. <th>{{$value->age}}</th>
  23. <th>
  24. @if($value->sex == 1)
  25. @else
  26. @endif
  27. </th>
  28. <th>{{$value->position}}</th>
  29. </tr>
  30. @endforeach
  31. </tbody>
  32. </table>
  33. </body>
  34. </html>

2. if elseif else

视图:people.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>查看政府人员</title>
  6. </head>
  7. <body>
  8. <table align="center">
  9. <caption>政府人员信息</caption>
  10. <thead>
  11. <tr>
  12. <th>姓名</th>
  13. <th>年龄</th>
  14. <th>状态</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. @foreach($people as $value)
  19. <tr>
  20. <th>{{$value->name}}</th>
  21. <th>{{$value->age}}</th>
  22. <th>
  23. @if($value->age < 30)
  24. 青年
  25. @elseif ($value->age <40)
  26. 中年
  27. @elseif ($value->age < 50)
  28. 中老年
  29. @else
  30. 老年
  31. @endif
  32. </th>
  33. </tr>
  34. @endforeach
  35. </tbody>
  36. </table>
  37. </body>
  38. </html>

3. while

视图:people.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>查看政府人员</title>
  6. </head>
  7. <body>
  8. <?php
  9. $i = 0;
  10. $count = count($people);
  11. ?>
  12. @while ($i<$count)
  13. <p>{{$people[$i]->name}}</p>
  14. <?php $i++;?>
  15. @endwhile
  16. </body>
  17. </html>

二、数据库基本操作

1. 查询

返回的是一个二维数组

  1. $data = DB::select('select * from `goods` where id>?',[2]);
  2. echo '<pre>' . print_r($data,true) . '</pre>';

2. 插入

返回的是布尔值

  1. $temp = DB::insert('insert into `goods` (name,price,color,content,addtime) value(?,?,?,?,?)',['三星note10',7999,'莫奈彩','三星新一代旗舰手机',1575907200]);
  2. if($temp){
  3. echo '成功添加一条数据';
  4. }else{
  5. echo '添加失败';
  6. }


3. 更新

返回的是受影响的行数

  1. $temp = DB::update('update `goods` set price=? where name=?',[6999,'三星note10']);
  2. echo '成功修改了' . $temp . '条数据';


4. 删除

返回的是受影响的行数

  1. $temp = DB::delete('delete from `goods` where name=?',['三星note10']);
  2. echo '成功删除了' . $temp . '条数据';


三、总结

学会了blade模板引擎的常用的结构,学会了laravel对数据库的简单增删查改操作。

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:laravel有三种方式操作数据库, 选择任何一种都可以
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学