Blogger Information
Blog 28
fans 0
comment 0
visits 16431
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模型实现查询和软删除+恢复--2018年05月27日
植树青年小江同志的博客
Original
599 people have browsed it

实例

<?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();
        }

        
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!