使用Loader:

<?php

namespace app\index\controller;

use think\Controller;

use think\Loader;

class Login extends Controller

{

  public function __construct()

  {

    parent::__construct();

    //data数据表的名字

    $this->data=Loader::model('data');

  }

  public function test()

  {

    $res=$this->data->getMenu();

    dump($res);

  }

}

使用 _initialize 代替__construct ,不同版本的tp5有时候_initialize不带下划线

<?php

namespace app\index\controller;

use think\Controller;

use think\Loader;

class Login extends Controller

{

  public function _initialize()

  {

    //parent::__construct();

    //data数据表的名字

    $this->data=Loader::model('data');

  }

  public function test()

  {

    $res=$this->data->getMenu();

    dump($res);

  }

}

//model模型里面的Data.php

<?php

namespace app\index\model;

use think\Db;

use think\Model;

class Data extends Model

{

  protected $table='data';

  public function getMenu()

  {

    $result=Db::name($this->table)->select();

    return $result;

  }

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-05-28
相关资源
相似解决方案