thinkphp5调用模型的方法

 时间:2026-04-23 23:55:16

1、首先通膨救过命令行生成一个模型文件

(将命令行切换到项目根目录)

php think make:model 模块名/模型名

例:think make:model admin/Admin

thinkphp5调用模型的方法

2、静态调用(注意数据库配置)

<?php

namespace app\admin\controller;


use app\admin\model\Admin;
class Login

{

    public function check()

    {

        // 静态调用

        $admin = Admin::get(1);

        return $admin->admin_name;

    }

}

thinkphp5调用模型的方法

thinkphp5调用模型的方法

3、实例化模型

<?php

namespace app\admin\controller;


use app\admin\model\Admin;
class Login

{

    public function check()

    {

        // 实例化模型

        $admin = new Admin;

        return $admin->where('id = 1')->value('admin_name');

    }

}

thinkphp5调用模型的方法

thinkphp5调用模型的方法

4、使用 Loader 类实例化(单例)

<?php

namespace app\admin\controller;


use think\Loader;
class Login

{

    public function check()

    {

        // 使用 Loader 类实例化(单例)

        $admin = Loader::model('Admin');

        $re = $admin -> where('id = 1') -> find();

        dump($re);

    }

}

thinkphp5调用模型的方法

thinkphp5调用模型的方法

5、使用助手函数`model`

<?php

namespace app\admin\controller;


class Login

{

    public function check()

    {

  爷露      // 或者使用助手函数`model`

    歌没膨    $admin = model('admin');

        return $admin::get(1);

    }

}

thinkphp5调用模型的方法

thinkphp5调用模型的方法

  • 华为AR28-11路由器每IP限速设置QOS限速
  • 手把手教你制作属于自己的工具箱(操作简单)
  • win8.1应用商店下载的应用卸载
  • VMware Workstation加密虚拟机
  • 怎么修改系统日志最大存储空间
  • 热门搜索
    牙龈发炎怎么办 手机怎么控制电脑 电脑无法关机怎么办 中秋节英语怎么说 沃尔沃xc60怎么样 wifi怎么修改密码 个人简历籍贯怎么填 颧骨高怎么办 苹果内存怎么清理 怎么样开淘宝网店