gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:29:43 26 4
gpt4 key购买 nike

我正在尝试将 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 文件,因为它在另一个页面上有效。但是,在这个上,它似乎无法找到“租金”表。

谢谢!!

最佳答案

大家好,谢谢大家的帮助!

我想通了。问题是 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寻求帮助!

关于php - Illuminate\Database\QueryException (2002) SQLSTATE[HY000] [2002] 没有这样的文件或目录 (SQL : select * from `rents` ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com