首页 > 解决方案 > 未找到基表或视图 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();
    });

在此处输入图像描述

标签: phpmysqllaravel

解决方案


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


推荐阅读