php - 未找到基表或视图 laravel 数据透视表
问题描述
帖子表看起来像。
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('slug')->unique();
$table->string('image')->default('default.png');
$table->text('body');
$table->boolean('is_approved')->default(false);
$table->boolean('status')->default(false);
$table->integer('view_count')->default(0);
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade')->unsigned()->index();
$table->timestamps();
});
用户表。
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->default(2);
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('image')->default('default.png');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->text('about')->nullable();
$table->rememberToken();
$table->timestamps();
});
运行 php artisan:migrate 时出现错误。
我找不到它。
有我的 post_users 表。
Schema::table('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
解决方案
在PostUsers
模型中定义表名
class PostUsers extends Model {
public $table = "post_users";
更新:
不应该
Schema::table('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
是
Schema::create('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
如果你要创建一个新表,你必须使用Schema::create
推荐阅读
- angular - 类型“FormArray”角度 6 上不存在属性“拼接”
- selector - Polymer2 Shadow dom 选择子元素
- javascript - Javascript表单验证返回错误错误
- eclipse - Eclipse EGit 未打开文件比较
- java - 从多模块maven项目的另一个模块下载代码的正确方法是什么?
- javascript - HTML5 - 文件名在媒体播放器中不起作用
- javascript - 如何在单选按钮上正确注册点击事件?
- php - 无法在 PHP 中获得正确的类型转换结果
- javascript - Date.toLocaleDateString() 在 Nodejs v10.14.2 上不起作用
- python - django - 陷入路径生成问题