Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:blade语法比其它引擎简单多了
视图:testloop.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>循环测试</title>
</head>
<body>
@for($i=0;$i < 5;$i++)
<div>{{$i}}</div>
<br>
@endfor
<hr>
@foreach($lists as $key => $val)
<div style="color:red;">{{$val['name']}} 价格:{{$val['price']}}</div>
@endforeach
<hr>
@while($item = array_shift($lists))
<div>{{$item['name']}}价格:{{$item['price']}}</div>
@endwhile
</body>
</html>
控制器:home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Class Home extends Controller{
public function testloop(){
$data['lists'] = array(
['name' => '山东-打卤面','price'=>30],
['name' => '山西-老陈醋','price'=>100],
['name' => '北京-烤鸭','price'=>80],
['name' => '四川-火锅','price'=>200],
);
return view('testloop',$data);
}
}
路由器:web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home/testloop','Home@testloop');
Home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Class Home extends Controller{
//查询操作
public function mysql(){
$data['admin_list'] = DB::select('select * from staff limit 5,:n',['n'=>5]);
//echo '<pre>';
//var_dump($res);
return view('mysql',$data);
}
//修改操作
public function updates(){
$res= DB::update('update staff set name = "张起灵" where id = 1002');
var_dump($res);
}
//新增操作
public function insert(){
$res = DB::insert('insert into staff(name,sex,age,salary,hiredate) values("王晓霞",0,25,5500,"2020-09-16"),("黄飞鸿",0,30,55000,"2020-09-16")');
var_dump($res);
}
//删除操作
public function delete(){
$res = DB::delete('delete from staff where id = 1003');
var_dump($res);
}
//链式操作
public function chain(){
//原生查询预处理
$res = DB::select('select * from staff limit 0,:n',['n'=>1]);
//$res = DB::select('select * from staff limit ?',[1]);
//链式调用
$res2 = DB::table('staff')->first();
echo '<pre>';
print_r($res);
print_r($res2);
}
}
mysql.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据库查询测试</title>
</head>
<body>
@foreach($admin_list as $key => $val)
<div>用户名:{{$val->name}}--{{$val->dept}}</div>
@endforeach
</body>
</html>
web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home/mysql','Home@mysql');
Route::get('/home/updates','Home@updates');
Route::get('/home/insert','Home@insert');
Route::get('/home/delete','Home@delete');
Route::get('/home/chain','Home@chain');