2014-11-08 59 views
0

我是PHP和Laravel的新手。我正在關注LaraCast教程,它創建了一個簡單的用戶表。路由功能然後獲取所有用戶並返回到瀏覽器。我可以在db中看到數據,但Laravel路由的結果總是空的。創建數據庫,表格等後需要重新啓動嗎?謝謝你的幫助。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

是的。因爲我想快速入門,所以我用流浪漢建立了自己的環境。只是爲了檢查,我還使用「root」作爲用戶名,它也返回一個空集。任何方式來調試? – 2014-11-08 22:11:09

回答

1
  1. debug設置爲config/local/app.phptrue

  2. 確保您正在檢查連接憑證app/config/local/database.php而不是app/config/database.php。如果您使用的宅基地,您應該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數據庫。

+0

編輯database.php的本地副本修復了錯誤。我遵循的課程不使用宅基地,他們配置了app/config/database.php。宅基地數據庫也恰好有一個空的用戶表:(。謝謝你的回答! – 2014-11-09 00:22:59