【问题标题】:Laravel, infinite loading when I try to display MySQL tablesLaravel,当我尝试显示 MySQL 表时无限加载
【发布时间】:2021-11-27 00:46:14
【问题描述】:

我想在 Laravel 视图中显示我的 MySQL 数据库的所有表。
但是当我打开网站时,会无休止地加载页面。
我该如何解决?

路线:

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/listen', function () {
    $groups = DB::select('SHOW TABLES');
    return view('listen', ['groups' => $groups]);
});

需要显示表格的页面:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="{{ asset('css/app.css') }}" rel="stylesheet">

        <title>ListenOnce - Homepage</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">

    </head>
    <body class="bg-dark">
        <div id="app">
            <h1 class="display-1 text-center" style="color: #8800e7; margin-top: 50px">Browse groups</h1>
            @foreach($groups as $group) 
                <div>
                    {{ $group->group_name }}
                </div>
            @endforeach
        </div>
        <script src="{{ mix('/js/app.js') }}"></script>
    </body>
</html>

数据库配置:

<?php

use Illuminate\Support\Str;

return [

    'default' => 'mysql',

    'connections' => [

        'mysql' => [
            'read' => [
                'host' => [
                    '127.0.0.1'
                ],
            ],
            'write' => [
                'host' => [
                    '127.0.0.1'
                ],
            ],
            'sticky' => true,
            'driver' => 'mysql',
            'database' => 'listenonce',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'port' => '8080'
        ],

    ],
];

我试图等待,并给出“超过 60 秒的最大执行时间”错误。
我没有使用默认的 .env 文件。
我该如何解决?
我是 Laravel 和 MySQL 的新手。
提前致谢!

【问题讨论】:

  • 您是否对您的代码进行过任何调试?在我看来,简单的第一步是路由定义中的dd($groups);
  • 为什么MySQL 有端口8080?应该是3306...8080 用于 HTTP。
  • 此外,你甚至不应该使用网络访问 MySQL,它应该被禁用并使用本地套接字。

标签: php mysql laravel database


【解决方案1】:

解决方案

首先,我将 MySQL 端口从 3306 更改为 8020。
然后我从这里更改了配置:

<?php

use Illuminate\Support\Str;

return [

    'default' => 'mysql',

    'connections' => [

        'mysql' => [
            'read' => [
                'host' => [
                    '127.0.0.1'
                ],
            ],
            'write' => [
                'host' => [
                    '127.0.0.1'
                ],
            ],
            'sticky' => true,
            'driver' => 'mysql',
            'database' => 'listenonce',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'port' => '8080'
        ],

    ],
];

到这里:

<?php

return [

    'default' => 'mysql',

    'connections' => [

        'mysql' => [
            'read' => [
                'host' => [
                    'localhost:8020'
                ],
            ],
            'write' => [
                'host' => [
                    'localhost:8020'
                ],
            ],
            'sticky' => true,
            'driver' => 'mysql',
            'database' => 'listenonce',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'port' => '8020'
        ],

    ],
];

然后我重新启动了 MySQL、Apache 和应用程序,终于可以工作了。

【讨论】:

    猜你喜欢
    • 2016-11-20
    • 2021-03-05
    • 2021-02-17
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多