简体   繁体   English

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

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

I'm trying to connect a Laravel project to an existing database.我正在尝试将 Laravel 项目连接到现有数据库。

I have followed the Eloquent Model Conventions ;我遵循了雄辩的示范公约 however, I am still hitting the following error:但是,我仍然遇到以下错误:

Illuminate \\ Database \\ QueryException (2002) SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from rents ) Illuminate \\ Database \\ QueryException (2002) SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL:从rents选择 *)

Here is my code:这是我的代码:

web.php网页.php

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

RentsController.php租金控制器.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

<?php

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


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

welcome.blade.php欢迎.blade.php

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

* One thing that may be the problem is that I am running the page locally (php artisan serve), while the database is real and online. * 可能有问题的一件事是我在本地运行页面(php artisan serve),而数据库是真实且在线的。 Would that cause the issue?这会导致问题吗? If so, any idea how to fix that?如果是这样,知道如何解决这个问题吗? * *

Any idea what could be the problem?知道可能是什么问题吗? I have the .env file setup correctly, as it works on another page.我的 .env 文件设置正确,因为它可以在另一个页面上运行。 However, on this one, it does not seem to be able to find the 'rents' table.但是,在这个上,它似乎无法找到“租金”表。

Thanks!!谢谢!!

Everyone, thank you for your help!各位,谢谢你们的帮助!

I figured it out.我想到了。 The problem was the DB_HOST.问题是 DB_HOST。

I am running the page locally (ie php artisan serve) and the database is online.我在本地运行页面(即 php artisan serve)并且数据库在线。 I do not have access to the database on my local computer.我无权访问本地计算机上的数据库。 This is why it couldn't connect.这就是它无法连接的原因。

So, instead of DB_HOST = localhost OR 127.0.0.1, I put it to the actual host.所以,我把它放到了实际的主机上,而不是 DB_HOST = localhost OR 127.0.0.1。

For example:例如:

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

Then, I needed to clear the cache in the terminal:然后,我需要清除终端中的缓存:

php artisan config:cache
php artisan cache:clear

Props to @ AddWeb Solution Pvt Ltd for the help!向@ AddWeb Solution Pvt Ltd寻求帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] 在文件 Illuminate/Database/Connection.php 上的第 678 行 - Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] in file Illuminate/Database/Connection.php on line 678 laravel SQLSTATE[HY000] [2002] 没有那个文件或目录(SQL: select * from `sessions` where `id` = - laravel SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL: select * 来自`jobs`) - SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `jobs`) SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL: select * 来自`jobs`) 11 - SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `jobs`) 11 SQLSTATE[HY000] [2002] 没有那个文件或目录 - SQLSTATE[HY000] [2002] No such file or directory Symfony SQLSTATE[HY000] [2002] 没有这样的文件或目录数据库连接 - Symfony SQLSTATE[HY000] [2002] No such file or directory database connection PDOException: SQLSTATE[HY000] [2002] 没有那个文件或目录 - PDOException: SQLSTATE[HY000] [2002] No such file or directory 错误:SQLSTATE[HY000] [2002] 没有那个文件或目录 - Error: SQLSTATE[HY000] [2002] No such file or directory Magento安装SQLSTATE [HY000] [2002]没有此类文件或目录错误 - Magento Installation SQLSTATE[HY000] [2002] No such file or directory error Laravel 4.1 to Heroku:SQLSTATE [HY000] [2002]没有这样的文件或目录 - Laravel 4.1 to Heroku: SQLSTATE[HY000] [2002] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM