How to make database query inside a config file?

后端 未结 3 1691
时光说笑
时光说笑 2020-12-22 02:08

I am struggling to figure out how I can make a database query inside of a Laravel config file.

I currently use:



        
3条回答
  •  青春惊慌失措
    2020-12-22 02:56

    You have a raw SQL query, and you are inside the config file. The config file has to be parsed before Laravel connects to the database.

    You can simply connect to the database in pure php and do the query.

    Connecting and querying a database in pure php is explained here: https://www.w3schools.com/php/php_mysql_connect.asp

    $conn = mysqli_connect($servername, $username, $password);
    $result = $conn->query('select version() as version');
    $row = $result->fetch_assoc();
    echo $row["version"];
    $conn->close();
    

    This should do it.

提交回复
热议问题