Blogger Information
Blog 21
fans 2
comment 3
visits 44154
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.24闭包实现查询
李洋
Original
722 people have browsed it

1、闭包查询

    

实例

 //闭包查询
    public function select()
    {
        //闭包实现查询
        $data = StaffModel::all(function ($query) {
            $query->where("salary", ">", 6000)->where("age", "<", 50);
        });
        dump($data);
        //在闭包内使用外部变量
        $age = $this->request->param("age") ?: 40;
        $salary = $this->request->param("salary") ?: 5000;

        //在闭包内使用外部变量必须要使用use引用,否则是不可用的
        $data = StaffModel::all(function ($query) use ($salary, $age) {
            $query->where("salary", ">", $salary)->where("age", "<", $age);
        });
        dump($data);
    }

运行实例 »

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


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