2014-10-03 83 views
0

林我routes.php文件,我有以下內容:Laravel基表或視圖沒有找到

<?php 

Route::group(array(), function() { 
    View::share('roots', Category::roots()->get()); 
    $tree = Category::get()->toHierarchy()->toArray(); 
    View::share('categories', $tree); 

    Route::get('/', array('as' => 'home', 'uses' => '[email protected]')); 
}); 

當我的數據庫沒有表還和我想要做的PHP工匠遷移 結果是:SQLSTATE [ 42S02]:基表或視圖未找到:1146表「ae_dev.categories」不存在

我的遷移文件:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateCategoriesTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() { 
    Schema::create('categories', function(Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('parent_id')->nullable()->index(); 
     $table->integer('lft')->nullable()->index(); 
     $table->integer('rgt')->nullable()->index(); 
     $table->integer('depth')->nullable(); 
     $table->string('name', 255); 

     $table->timestamps(); 
    }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() { 
    Schema::drop('categories'); 
    } 

} 

我認爲Laravel托盤從呼叫路由類別。 PHP和想要做選擇或思考,所以我想運行的遷移創建類別表,但上述錯誤產生前...

我該如何解決這個問題?

+0

你讀過[本章](http://laravel.com/docs/4.2/schema) – worldask 2014-10-03 07:58:52

+0

好像你有嘗試訪問數據庫的東西;你是否向你的工匠添加了任何命令或服務? – Repox 2014-10-03 08:07:23

+0

不,我沒有向工匠添加任何新的命令或服務......問題不在遷移方面,我認爲,但你是對的... Route.php中的類別想要訪問數據庫,但爲什麼它接觸routes.php,如果我只想從命令行運行'php artisan'... – 2014-10-03 08:45:26

回答

5

看來,所有php artisan命令使用routes.php文件,所以當你試圖在這個文件中訪問數據庫表(並且表不存在,因爲你沒有運行遷移),你會得到這個錯誤。你不能運行php artisan migrate,因爲你得到這個錯誤。

一個解決方案是刪除查詢數據庫的代碼,但它當然不是很好的解決方案。所以,你應該做的是從你做的第一個遷移選擇表(在你的情況下,它可能是categories後來你有更多的遷移,但是這將是第一個!),並添加類似的東西:

if (!Schema::hasTable('categories')) 
{ 
    return; 
} 

到您的routes.php文件。

但是,如果你將扮演更與遷移,並還需要其他的表來查詢你需要改變上述條件,例如爲:

if (!Schema::hasTable('categories') || !Schema::hasTable('users')) 
{ 
    return; 
} 

,但它仍然會造成一些問題 - 你寧願不要「不想每次在你的路線運行這段代碼,所以我會做這種方式:

if ($env == 'local') { 

    if (!Schema::hasTable('categories') || !Schema::hasTable('users')) 
    { 
     return; 
    } 
} 

你需要配置你的課程環境。但是現在您僅爲本地環境運行此代碼,在生產時,此代碼將不會運行,因此不會影響應用程序的性能。當然我在這裏假設你不會和生產工匠一起玩。

編輯

,但如果你使用的查詢只對共享數據(而不是路由),我會提出這些線路:

View::share('roots', Category::roots()->get()); 
$tree = Category::get()->toHierarchy()->toArray(); 
View::share('categories', $tree); 

BaseController和run方法(或父類的構造)在擴展它的所有控制器中。

老的回答(在這種情況下不充分)

錯誤信息是與你的解釋,你必須在你的數據庫中沒有表還不夠清楚。如果您沒有表格,則無法運行數據庫查詢。

在你遷移你應該創建例如必要的表,這是對用戶表遷移:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class NewUser extends Migration 
{ 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('users', function ($table) { 
      $table->engine = 'InnoDB'; 
      $table->increments('id'); 
      $table->string('user_name', 60)->unique(); 
      $table->string('email', 120)->unique(); 
      $table->string('passwd', 256); 
      $table->decimal('balance', 8, 2); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 

    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     // 
     Schema::drop('users'); 
    } 

} 

使用遷移時(或許你現在做的),你應該也不會將數據插入到數據庫中。你應該這樣做,而seeding tables

+0

我有遷移,但我無法運行它... – 2014-10-03 08:15:07

+0

@KarolFiturski你在遷移中有什麼?向我們展示代碼 – 2014-10-03 08:16:00

+0

我添加了遷移源 – 2014-10-03 08:35:48

相關問題