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加密虚拟机
  • 怎么修改系统日志最大存储空间
  • 热门搜索
    宝宝头发稀少怎么办 微笑面对生活作文 2元一次方程怎么解 生活秘书 如何预防流感 诚信让生活更美好作文 道客巴巴怎么复制 qq独立密码怎么设置 生活的感悟 喉咙干燥是什么原因