Blogger Information
Blog 43
fans 0
comment 0
visits 26790
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp数据库操作方法+2018年10月25日
Lee的博客
Original
946 people have browsed it

完成模型中的CURD操作,全部采用静态方法;


实例

<?php
namespace app\index\controller;
use app\model\User;
use traits\controller\Jump;
class Demo2
{
    use Jump;
    public function getOne(){
//        $res = User::get(1);
//        dump($res);

        $salary = 5000;
        $res = User::all(function ($query)use ($salary){
            $query->where('age','>',25)
                ->where('salary','>',$salary);
        });
        foreach ($res as $user){
            echo $user->name,'->',$user->salary,'<br>';
        }
    }

    public function getAll(){
        $salary = 5000;
        $res = User::all(function ($query)use ($salary){
            $query->where('age','>',25)
                ->where('salary','>',$salary);
        });
        dump($res);
    }

    public function update(){
        User::update(['age'=>33],['id'=>4]);
        $this->success('更新成功','show');
    }

    public function show(){
        dump(User::where('id',4)->field('id,age,name')->select());
    }
}

运行实例 »

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




练习视图中的assign()和fetch()方法


实例

<?php
namespace app\index\controller;
use think\facade\View;
use app\model\User;
class Demo3
{
    public function index(){
        $site = '小渣渣';
        $user = User::get(4);
        $list = User::all();


        View::assign('lee',$site);
        View::assign('user',$user);
        View::assign('list',$list);


        return View::fetch('index');
    }
}

运行实例 »

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



试着找一个后台模块,并将首页导入到框架项目中。


实例

<?php
namespace app\index\controller;
use think\facade\View;
class Index
{
    public function index()
    {
        return View::fetch('index');
    }

    public function welcome(){
        return View::fetch('welcome');
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}

运行实例 »

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


QQ图片20181028175824.png


总结,后台已完成,感觉有模版,代码非常容易!


Correction status:Uncorrected

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