批改状态:合格
老师批语:
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Staff as StaffModel;
use think\mode\concern\softDelete;
class Closure extends Controller
{
public function read()
{
// 闭包查询
$id = $this->request->param('staff_id') ? : 5;
$staff = StaffModel::all(function ($query) {
$query->where('staff_id', '>', $id);
});
dump($staff);
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request)
{
//更新
StaffModel::update(
['salary' => \think\Db::raw('salary=0')],
function ($query) {
$query->where('name', '李小璐');
}
);
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
// 删除数据
StaffModel::destroy(function ($query) {
$query->where('name', '贾乃亮');
});
}
public function create()
{
// 增加数据
$data = [
'name' => '贾乃亮',
'sex' => 0,
'age' => 33,
'salary' => 91,
];
$filed = ['name', 'sex', 'age', 'salary'];
StaffModel::create($data, $field);
}
public function softDelete() {
// 软删除和恢复操作
// StaffModel::destroy(function ($query) {
// $query->where('salary', '<', 1000);
// });
StaffModel::destroy(37);
$staff = StaffModel::withTrashed()->select();
dump($staff);
echo '<br>';
$res = StaffModel::onlyTrashed()->select();
foreach($res as $data) {
$data->restore();
}
}
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号