【发布时间】:2018-08-26 16:50:13
【问题描述】:
我正在尝试将 Laravel 项目连接到现有数据库。
我关注了Eloquent Model Conventions;但是,我仍然遇到以下错误:
Illuminate \ Database \ QueryException (2002) SQLSTATE[HY000] [2002] 没有这样的文件或目录(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是否存在?您是否能够连接到任何其他数据库表? -
好问题,感谢您的快速回复。是的,“租金”表确实存在。不,很遗憾,我无法连接到其他任何表。
-
谢谢@IzzEps,我也已经看过那个页面了。不幸的是,这些建议都无济于事。我想这里独特的一件事是我试图连接到一个已经存在的数据库,而不是通过 Laravel 本身创建它。在这种情况下,我不需要任何迁移。
标签: php mysql laravel laravel-5.4