thinkphp5调用模型的方法

2025-10-07 08:25:41

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢