Symfony\Component\Debug\Exception\FatalErrorException Class 'Comments' not found

29,705

Solution 1

  1. composer dump-autoload
  2. composer update
  3. Migration file name is in the right format (YYYY_MM_DD_HHMMSS_filename.php)

Solution 2

May be your model name will be "Comment" not "Comments". Because laravel uses the plural form of the name fo model as the default database. Such as User model for users table, Comment model for comments table.

Solution 3

Try including use App/Comments in your controller file. I hope it will work, and if it dont work then check for any spelling mistake and also double check your ROUTES .

Share:
29,705
Eiður Geir Vilhelmsson
Author by

Eiður Geir Vilhelmsson

Updated on July 13, 2020

Comments

  • Eiður Geir Vilhelmsson
    Eiður Geir Vilhelmsson almost 4 years

    My model Comments.php: I'm not including the class anywhere I just created the model and the migration and nothing works, I am so confused I can't think straight.

    <?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');
            }
    }
    

    And here is my migration:

    <?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');
        }
    
    }
    

    Now once I run the command "php artisan migrate:rollback and /or refresh" I get this error code:

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

    Only way for me to refresh the tables is to manually drop all tables with phpMyAdmin and run "php artisan migrate"

    But I don't know if that is healthy.