TestModel.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;;
class TestModel extends Model
{
protected $table = 'test';
public static function index() {
$configs = DB::select('select * from tests');
}
}
这是我的代码。我有/ public文件夹名称Test.php中的文件,并希望使用DB。但是当我在Test.php中调用index()函数时,它会返回错误。
PHP Fatal error: Uncaught RuntimeException: A facade root has not been set
Test.php保存在公共文件夹中
use App\Http\Models\TestModel;
$configs = TestModel::index();
请帮忙。谢谢!
答案 0 :(得分:0)
当您在不调用index.php的情况下在公用文件夹中使用任何内容时,它将不会启动数据库以便克服此问题:
/**
* Using For Database Configuration
*/
define('DB_TYPE', 'mysql');
define('DB_USER', 'username');
define('DB_HOST', 'localhost');
define('DB_PORT', '3306');
define('DB_PASSWORD', '');
define('DB_NAME', 'db-name');
$test = new TestModel;
$test->addConnection([
'driver' => DB_TYPE,
'host' => DB_HOST,
'port' => DB_PORT,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]);
$test->setAsGlobal();
$test->bootEloquent();
$configs = $test->index();
此外,您必须在TestModel.php中替换一行 替换使用Illuminate \ Support \ Facades \ DB ;;使用Illuminate \ Database \ Capsule \ Manager作为DB;
现在,当您运行此命令时,这将为您工作。