thinkphp5调用模型的方法

 时间:2026-02-15 23:28:52

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加密虚拟机
  • 怎么修改系统日志最大存储空间
  • 热门搜索
    劫难的意思 save是什么意思 什么是地震烈度 芙蓉楼送辛渐的意思 什么是soa 顺顺溜溜的意思 长歌行古诗意思解释 锐不可当的意思 空调显示h1是什么意思 shock是什么意思