我是PHP和Laravel的新手。我正在关注LaraCast教程,它创建了一个简单的用户表。然后路由功能获取所有用户并返回浏览器。我可以在数据库中看到数据,但结果总是通过Laravel路由为空。创建db,table等后是否需要重新启动任何内容?谢谢你的帮助。
routes.php文件
Route::get('/', function(){
$users = DB::table('users')->get();
return $users;
});
database.php中
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'awesomeproject',
'username' => 'homestead',
'password' => '***',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
当我点击http://awesome.app:8000/时,我得到[]作为回复。
MySQL的
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| awesomeproject |
| homestead |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
mysql> use awesomeproject
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--------------------------+
| Tables_in_awesomeproject |
+--------------------------+
| users |
+--------------------------+
1 row in set (0.00 sec)
mysql> select * from users;
+----------+---------------+
| username | password |
+----------+---------------+
| ulidder | skjdfsjkhsdfs |
| user2 | dslfjasdfhw |
+----------+---------------+
2 rows in set (0.00 sec)
答案 0 :(得分:1)
debug
中true
设置为config/local/app.php
吗?
确保您检查app/config/local/database.php
中的连接凭据,而不是app/config/database.php
。如果您使用的是Homestead,那么app/config/local/database.php
应该是这样的:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
如果您未在app/config/local/database.php
中设置凭据,则当您认为正在查询homestead
数据库时,可能会从awesomeproject
数据库中提取结果。