批改状态:合格
老师批语:
5月24日作业:
闭包函数:
$staff = StaffM::all(function($query){
$query -> where('id','<=',9)
-> where('salary','<',8000)
-> order('id DESC')
->limit(3);
});
dump($staff);
1、staff 模型 app\test\model\staff
<?php
namespace app\test\model;
//window 窗口: 自动创建index/model/Staff.php
//方法:D:\PHPTOOL\myphp_www\PHPTutorial\WWW\www.51.io>php think make: model test/Staff
use think\Model;
use think\model\concern\SoftDelete;
class Staff extends Model
{
//使用软删除
use SoftDelete;
//设置数据表明
protected $table = 'staff';
//设置主键
protected $pk = 'id';
//设置删除时间字段(在建数据表时完成),以实现软删除功能
protected $deletetime = 'delete_time';
//设置软删除字段的默认值(在建数据表时完成)
protected $defaultSoftDelete = 0;
}点击 "运行实例" 按钮查看在线实例
2、staff 控制器 app\test\controller\staff
<?php
namespace app\test\controller;
//window 窗口: 自动创建index/controller/Staff.php
// 方法: D:\PHPTOOL\myphp_www\PHPTutorial\WWW\www.51.io>php think make controller test/Staff
use think\Controller;
use think\Request;
//设置别名,防止与当前控制器重名
use app\test\model\Staff as StaffM;
class Staff extends Controller
{
/**
* 显示资源列表
* @return \think\Response
*/
public function index()
{
//显示数据表的单条数据
$staff = StaffM::get(3);
dump($staff);
}
/**
* 显示创建资源表单页.
* @return \think\Response
*/
public function create()
{
//添加数据
$data = ['name'=>'张三', 'salary'=>5700];
//添加的字段,其他用默认值
$field = ['name', 'salary'];
$staff = StaffM::create($data, $field);
}
/**
* 保存新建的资源
* @param \think\Request
* @return \think\Response
*/
public function save(StaffM $staff)
{
//新增数据
$staff -> name = 'frank zou';
$staff -> salary = 12000;
$staff -> save();
}
/**
* 保存更新的资源
* @param \think\Request
* @return \think\Response
*/
public function update()
{
//更新数据
StaffM::update(
['name' => '柳柳'],
['id' => 3]
);
}
/**
* 查询资源
* @param \think\Request
* @return \think\Response
*/
public function all()
{
//数据查询
$staff = StaffM::all([7,12,22]);
dump($staff);
echo '<hr>';
// $func = function($query){
// $query -> where('age','<=',50)
// ->where('salary','>',3000);
// }
$staff = StaffM::all(function($query){
$query -> where('id','<=',9)
-> where('salary','<',8000)
-> order('id DESC')
->limit(3);
});
dump($staff);
}
/**
* 删除指定资源
* @param int $id
* @return \think\Response
*/
public function delete()
{
StaffM::destroy([21, 22]);
}
/**
* 软删除资源
* @param int $id $query
* @return \think\Response
*/
public function softDelete()
{
//StaffM::destroy(5);
//设置时间标志位,delete_time
//[ SQL ] UPDATE `staff` SET `delete_time` = 1527233750 WHERE `id` = 5
//查看不包括软删除的记录
$sum = StaffM::where('id < 6')
-> select();
dump($sum);
echo '<hr>';
//查看包括软删除的记录
$sum = StaffM::withTrashed()
-> where('id < 6')
->where('salary > 2000')
->order('id DESC')
->limit(3)
-> select();
dump($sum);
echo '<hr>';
//查看回收站
$sum = StaffM::onlyTrashed()
-> select();
dump($sum);
}
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号