Illuminate \ Database \ QueryException (2002) SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL: select * 来自`rents`)

Illuminate \ Database \ QueryException (2002) SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `rents`)

我正在尝试将 Laravel 项目连接到现有数据库。

我已经关注了Eloquent Model Conventions;但是,我仍然遇到以下错误:

Illuminate \ Database \ QueryException (2002) SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from rents)

这是我的代码:

web.php

Route::get('/', [
    'uses' => 'RentsController@index'
    ]);

RentsController.php

<?php

namespace App\Http\Controllers;

use App\Rent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class RentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        $rents = Rent::all();
        return view('welcome', ['rents' => $rents]);
    }

    ...
}

Rent.php

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;


class Rent extends Model {
    protected $table = 'rents';
}

welcome.blade.php

<!doctype html>
<html>
    <head>
    </head>
    <body>
        <ul>
            @foreach ($rents as $rent)
                <li>{{ $rent->title }}</li>
            @endforeach
        </ul>
    </body>
</html>

* 可能是问题的一件事是我在本地 运行 页面(php artisan serve),而数据库是真实的在线的。这会导致问题吗?如果是这样,知道如何解决吗? *

知道可能是什么问题吗?我正确设置了 .env 文件,因为它在另一个页面上有效。然而,在这个上,它似乎无法找到 'rents' table.

谢谢!!

各位,谢谢大家的帮助!

我明白了。问题是 DB_HOST.

我是 运行 本地页面(即 php artisan serve)并且数据库在线。我无权访问本地计算机上的数据库。这就是无法连接的原因。

所以,我把它放到实际主机上,而不是 DB_HOST = localhost OR 127.0.0.1。

例如:

DB_HOST=hostname_from_server
DB_PORT=3306
DB_DATABASE=database_name_on_server
DB_USERNAME=username_on_server
DB_PASSWORD=password_on_server

然后,我需要清除终端中的缓存:

php artisan config:cache
php artisan cache:clear

向@AddWeb Solution Pvt Ltd 求助!