帖子表看起来像
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();
});
答案 0 :(得分:2)
在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