博客列表 >blade模板引擎语法和数据库原生查询

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

王佳祥
王佳祥原创
2020年09月16日 17:19:561115浏览

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');
  • 原生查询:


  • 原生修改:


  • 原生新增:


  • 原生删除:


  • 链式调用:


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议