我在Laracasts中从头开始学习Laravel 5系列,具体来说,我正在学习"获取数据"课程。
我创建了database.sqlite文件,从修补程序中添加了一些数据,并能够在控制台中检索它们。然后,我试着复制视频的作用。
我的卡控制器:
<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Http'Controllers'Controller;
use App'Http'Requests;
class CardsController extends Controller
{
public function index()
{
$cards = 'DB::table('cards')->get();
return view('cards.index', compact('cards'));
}
}
然而,当我尝试加载/卡路由时,我有以下错误
InvalidArgumentException in SQLiteConnector.php line 34:
Database (homestead) does not exist.
这是我的env
文件
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:P3ZgRMRkb2e8+x7S9rDLLB+bKJdR5Unpj8zXBUIHIZE=
APP_URL=http://localhost
DB_CONNECTION=sqlite
DB_FILE=database.sqlite
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
在错误中引用的行下,是以下内容:
// Here we'll verify that the SQLite database exists before going any further
// as the developer probably wants to know if the database exists and this
// SQLite driver will not throw any exception if it does not by default.
if ($path === false) {
throw new InvalidArgumentException("Database (${config['database']}) does not exist.");
}
查看该方法上面的注释,您的路径似乎设置不正确。我认为正确的环境变量应该是:
DB_DATABASE=database/database.sqlite
而不是
DB_FILE=database/database.sqlite
如果没有,请检查您的config/database.php
文件,并查找以下部分:
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')), // this line!
'prefix' => '',
],
我刚刚注意到(至少在mac上的Laravel 5.2.29版本中)路径必须是绝对路径。奇怪的是,对于运行迁移,一个相对路径会起作用,但在之后尝试访问DB时应用程序会失败