0
votes

enter image description here

When i migrate my tables into db, it gives this error

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment primary key, body longtext not null, url varchar(255) null, user_id int unsigned not null, commentable_id int unsigned not null, commentable_type varchar(191) not null, created_a t timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci) In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(!Schema::hasTable('users')){
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table-> string('name');
                $table-> string('email')->unique();
                $table-> string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }
        Schema::table('users', function(Blueprint $table){
            $table -> string('first_name') -> nullabel();
            $table -> string('middle_name') -> nullabel();
            $table -> string('last_name') -> nullabel();
            $table -> string('city') -> nullabel();
            $table -> integer('role') -> unsigned();
        });
    }

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

I dropped all the tables from database then i tried but same error given

2
try this code: php artisan migrate:freshFarhad Misirli
i did but didn't work for meSayed mujahid
Please post your migrations, are you defining the user table twice?Ryan J Field
Check your .env file and see if the database name is what it should be.Rouhollah Mazarei

2 Answers

0
votes

Try either splitting into two migrations or adding all fields at once:

Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table-> string('name');
                $table-> string('email')->unique();
                $table-> string('password');
                $table->rememberToken();
                $table->timestamps();
                $table -> string('first_name') -> nullable();
                $table -> string('middle_name') -> nullable();
                $table -> string('last_name') -> nullable();
                $table -> string('city') -> nullable();
                $table -> integer('role') -> unsigned();
            });

Please note: you have also spelt nullable wrong.

0
votes

Probably you are using Laravel 5.5 so you must have make "php artisan migrate" without edit your AppServiceProvider.php file inside the boot method.

So I recomend you to delete your tables from the database with "php artisan tinker" Then "Schema::drop('users')(and exit with q)".

After that you have to edit your "AppServiceProvider.php" file, so use this link to help you: https://laravel-news.com/laravel-5-4-key-too-long-error

When when you have already edited the file you can now make the "php artisan migrate:rollback" and after that "php artisan migrate"

that works with me !