Blogger Information
Blog 55
fans 3
comment 0
visits 54984
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
blade模板引擎语法和数据库原生查询
王佳祥
Original
1126 people have browsed it

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


  • 原生修改:


  • 原生新增:


  • 原生删除:


  • 链式调用:


Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:blade语法比其它引擎简单多了
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