我的模型Comments.php:我没有把这个类包括在我刚刚创建的模型和迁移中,没有任何作用,我很困惑,我不能直接思考 .
<?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 artisan migrate:rollback and /或refresh”,我得到此错误代码:
“[Symfony \ Component \ Debug \ Exception \ FatalErrorException]未找到类'评论'”
刷新表的唯一方法是使用phpMyAdmin手动删除所有表并运行“php artisan migrate”
但我不知道这是否 Health .
3 回答
composer dump-autoload
composer update
迁移文件名格式正确(
YYYY_MM_DD_HHMMSS_filename.php
)可能是您的型号名称将是“评论”而不是“评论” . 因为laravel使用模型名称的复数形式作为默认数据库 . 例如用户模型用户表,评论模型用于评论表 .
尝试在控制器文件中包含
use App/Comments
. 我希望它会工作,如果它不工作,那么检查任何拼写错误,并仔细检查你的 ROUTES .