mysqllaraveldatabasemigration

Even if i migrate:fresh it shows me that the table already exists.. why?


so i want to php artisan migrate:fresh but i get this error

Base table or view already exists: 1050 Table 'roles' already exists

even if i drop the database from phpmyadmin, clean the cache and create the database again it still show the same message the migration for the table rows is the following one:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesTable extends Migration
{
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->tinyInteger('status');
        });
    }

    public function down()
    {
        Schema::dropIfExists('roles');
    }
}

the full error displayed:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'roles' already exists (SQL: create table roles (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, guard_name varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

Why is that? and what can or should I do?


Solution

  • First drop roles table using this code Schema::dropIfExists('roles'); then create.

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateRolesTable extends Migration
    {
        public function up()
        {
            Schema::dropIfExists('roles'); //added
            Schema::create('roles', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->tinyInteger('status');
            });
        }
    
        public function down()
        {
            Schema::dropIfExists('roles');
        }
    }