登录  /  注册
博主信息
博文 55
粉丝 3
评论 0
访问量 66692
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
blade模板引擎语法和数据库原生查询
王佳祥
原创
1650人浏览过

blade模板引擎语法和数据库原生查询

一、blade语法:循环

视图:testloop.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. </head>
  8. <body>
  9. @for($i=0;$i < 5;$i++)
  10. <div>{{$i}}</div>
  11. <br>
  12. @endfor
  13. <hr>
  14. @foreach($lists as $key => $val)
  15. <div style="color:red;">{{$val['name']}} 价格:{{$val['price']}}</div>
  16. @endforeach
  17. <hr>
  18. @while($item = array_shift($lists))
  19. <div>{{$item['name']}}价格:{{$item['price']}}</div>
  20. @endwhile
  21. </body>
  22. </html>

控制器:home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. Class Home extends Controller{
  5. public function testloop(){
  6. $data['lists'] = array(
  7. ['name' => '山东-打卤面','price'=>30],
  8. ['name' => '山西-老陈醋','price'=>100],
  9. ['name' => '北京-烤鸭','price'=>80],
  10. ['name' => '四川-火锅','price'=>200],
  11. );
  12. return view('testloop',$data);
  13. }
  14. }

路由器:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/', function () {
  4. return view('welcome');
  5. });
  6. Route::get('/home/testloop','Home@testloop');


二、数据库原生查询(增删改查,预处理)和链式调用

  • 控制器:Home.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //查询操作
  7. public function mysql(){
  8. $data['admin_list'] = DB::select('select * from staff limit 5,:n',['n'=>5]);
  9. //echo '<pre>';
  10. //var_dump($res);
  11. return view('mysql',$data);
  12. }
  13. //修改操作
  14. public function updates(){
  15. $res= DB::update('update staff set name = "张起灵" where id = 1002');
  16. var_dump($res);
  17. }
  18. //新增操作
  19. public function insert(){
  20. $res = DB::insert('insert into staff(name,sex,age,salary,hiredate) values("王晓霞",0,25,5500,"2020-09-16"),("黄飞鸿",0,30,55000,"2020-09-16")');
  21. var_dump($res);
  22. }
  23. //删除操作
  24. public function delete(){
  25. $res = DB::delete('delete from staff where id = 1003');
  26. var_dump($res);
  27. }
  28. //链式操作
  29. public function chain(){
  30. //原生查询预处理
  31. $res = DB::select('select * from staff limit 0,:n',['n'=>1]);
  32. //$res = DB::select('select * from staff limit ?',[1]);
  33. //链式调用
  34. $res2 = DB::table('staff')->first();
  35. echo '<pre>';
  36. print_r($res);
  37. print_r($res2);
  38. }
  39. }
  • 视图:mysql.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. </head>
  8. <body>
  9. @foreach($admin_list as $key => $val)
  10. <div>用户名:{{$val->name}}--{{$val->dept}}</div>
  11. @endforeach
  12. </body>
  13. </html>
  • 路由:web.php
  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/', function () {
  4. return view('welcome');
  5. });
  6. Route::get('/home/mysql','Home@mysql');
  7. Route::get('/home/updates','Home@updates');
  8. Route::get('/home/insert','Home@insert');
  9. Route::get('/home/delete','Home@delete');
  10. Route::get('/home/chain','Home@chain');
  • 原生查询:


  • 原生修改:


  • 原生新增:


  • 原生删除:


  • 链式调用:


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

批改状态:合格

老师批语:blade语法比其它引擎简单多了
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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

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