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调用模型的方法

© 2026 一点知道
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com