skip to Main Content

articles table

public function up()
    {
        Schema::create('Articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->string('body');
            $table->timestamps();
            $table->foreign('user_id')->references('id')
                ->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('Articles');
    }

tags table

    public function up()
    {
        Schema::create('tags', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
        Schema::create('article_tag',function (Blueprint $table)
        {

            $table->integer('article_id')->unsigned()->index();
            $table->foreign('article_id')->references('id')->
                on('articles')->onDelete('cascade');

            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('tag_id')->references('id')->
                on('tags')->onDelete('cascade');

            $table->timestamps();
        });
    }

i want to make tags table in phpmyadmin fut faced to this error after php artisan migrate command

error

`$ php artisan migrate
Migrating: 2020_04_01_195718_create_articles_table

IlluminateDatabaseQueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table ‘articles’ already exists (SQL: create table Articles (id bigint unsigned not null auto_increment prim
ary key, user_id int unsigned not null, title varchar(255) not null, body varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate ‘utf8mb4_un
icode_ci’)`

3

Answers


  1. try this code for user_id table

    Schema::create('Articles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('title');
        $table->string('body');
        $table->timestamps();
        $table->foreign('user_id')->references('id')
            ->on('users')->onDelete('cascade');
    });
    
    Login or Signup to reply.
  2. Use To Migrate All Tables

    php artisan migrate:refresh
    

    If you don’t want to loss your another tables data

    Just goto mysql or phpmyadmin then delete the table and also from migrations

    then re-run

    php artisan migrate
    
    Login or Signup to reply.
  3. try this

      public function up()
        {
            Schema::create('tags', function (Blueprint $table)
            {
                $table->bigIncrements('id');
                $table->string('name');
                $table->timestamps();
            });
            Schema::create('article_tag',function (Blueprint $table)
            {
    
                $table->unsignedBigInteger('article_id');
                $table->foreign('article_id')->references('id')->
                    on('articles')->onDelete('cascade');
    
                $table->unsignedBigInteger('tag_id');
                $table->foreign('tag_id')->references('id')->
                    on('tags')->onDelete('cascade');
    
                $table->timestamps();
            });
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search