2015-11-02 110 views
2

我的模型評論.php: 我沒有在任何地方包括類我剛創建模型和遷移,沒有任何作品,我很困惑我不能直視。Symfony Component Debug Exception FatalErrorException類'評論'未找到

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Comments extends Model { 

    protected $fillable = [ 
      'id', 
      'user_id', 
      'post_id', 
      'comment' 
     ]; 

     public function setPublishedAtAttribute($date) 
     { 
      $this->attributes['pubslished_at'] = Carbon::parse($date); 
     } 

     public function user() 
     { 
      return $this->belongsTo('App\User'); 
     } 
     public function posts() 
     { 
      return $this->belongsTo('App\Posts'); 
     } 
} 

這裏是我的移民:

<?php 

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

class CreateCommentsTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('comments', function(Blueprint $table) 
     { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned(); 
     $table->integer('post_id')->unsigned(); 
     $table->text('comment'); 
     $table->timestamps(); 
     $table->foreign('user_id') 
        ->references('id') 
        ->on('users') 
        ->onDelete('cascade'); 
     $table->foreign('post_id') 
       ->references('id') 
        ->on('posts') 
        ->onDelete('cascade'); 
     }); 
    } 

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

} 

現在,當我運行命令 「PHP工匠遷移:回滾和/或刷新」 我得到這個錯誤代碼:

[Symfony \ Component \ Debug \ Exception \ FatalErrorException] Class'Comments'Not found

刷新表格的唯一方法是用phpMyAdmin手動刪除所有表格並運行「php artisan migrate」

但我不知道這是否健康。

+1

你能測試一個路徑遷移後的作品?另外,啓用調試並檢查日誌中的錯誤描述。 –

+3

嘗試先做一個作曲家dump-autoload -o然後php artisan migrate:回滾和/或刷新。此外,Laravel希望你的模型能夠以單數「評論」和你的表格複數形式「評論」,如果你想保留你的模型爲「評論」添加保護$ table =「comments」 –

+1

我不知道爲什麼,但「作曲家轉儲-autoload -o「命令修復了我的問題,請仔細說明它是如何工作的?這是一個全新的命令:) 也謝謝;) –

回答

0

可能是您的型號名稱將是「評論」而不是「評論」。因爲laravel使用名稱fo模型的複數形式作爲默認數據庫。如用戶模型爲用戶表,評論模型爲評論表。

4
  1. composer dump-autoload
  2. composer update
  3. 遷移文件的名稱是正確的格式(YYYY_MM_DD_HHMMSS_filename.php
相關問題