一、blade模板引擎
控制器:Officer.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Officer extends Controller
{
public function find()
{
$data = DB::table('staffs')->get()->toArray();
return view('index.people',['people'=>$data]);
}
}
路由
Route::get('/index/show','Officer@find');
1. foreach
视图:people.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看政府人员</title>
</head>
<body>
<table align="center">
<caption>政府人员信息</caption>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>职位</th>
</tr>
</thead>
<tbody>
@foreach($people as $value)
<tr>
<th>{{$value->name}}</th>
<th>{{$value->age}}</th>
<th>
@if($value->sex == 1)
男
@else
女
@endif
</th>
<th>{{$value->position}}</th>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
2. if elseif else
视图:people.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看政府人员</title>
</head>
<body>
<table align="center">
<caption>政府人员信息</caption>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>状态</th>
</tr>
</thead>
<tbody>
@foreach($people as $value)
<tr>
<th>{{$value->name}}</th>
<th>{{$value->age}}</th>
<th>
@if($value->age < 30)
青年
@elseif ($value->age <40)
中年
@elseif ($value->age < 50)
中老年
@else
老年
@endif
</th>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
3. while
视图:people.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看政府人员</title>
</head>
<body>
<?php
$i = 0;
$count = count($people);
?>
@while ($i<$count)
<p>{{$people[$i]->name}}</p>
<?php $i++;?>
@endwhile
</body>
</html>
二、数据库基本操作
1. 查询
返回的是一个二维数组
$data = DB::select('select * from `goods` where id>?',[2]);
echo '<pre>' . print_r($data,true) . '</pre>';
2. 插入
返回的是布尔值
$temp = DB::insert('insert into `goods` (name,price,color,content,addtime) value(?,?,?,?,?)',['三星note10',7999,'莫奈彩','三星新一代旗舰手机',1575907200]);
if($temp){
echo '成功添加一条数据';
}else{
echo '添加失败';
}
3. 更新
返回的是受影响的行数
$temp = DB::update('update `goods` set price=? where name=?',[6999,'三星note10']);
echo '成功修改了' . $temp . '条数据';
4. 删除
返回的是受影响的行数
$temp = DB::delete('delete from `goods` where name=?',['三星note10']);
echo '成功删除了' . $temp . '条数据';
三、总结
学会了blade模板引擎的常用的结构,学会了laravel对数据库的简单增删查改操作。