0

Trying to migrate a users table. Already have 1 table at DB. It's Contacts for contact form. Trying to migrate a users table. Created my table with terminal $ php artisan make:migration create_users_table
Had the following code in it.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('username')->unique();
            $table->timestamp('email_verified_at')();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
         });
    }

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

When I tried to migrate the table terminal returns an error.

I run php artisan migrate and it returns Migrating: create_contacts_table

"I'm trying to migrate users table and because of the contacts table already exist it says table already exists : 1050 ... "

Tried to give an argument as it said on the website but didn't work either. I tried this $ php artisan migrate [--path[C:\xampp\htdocs\custom\database\migrations\2022_05_03_121341_create_users_table.php]]

but it returned No arguments expected for 'migrate' command

How can I change the directory of migrate command? Or how can I solve this problem.

NervousDev
  • 87
  • 1
  • 1
  • 13
  • Migrating specific migration is done with following command: `php artisan migrate --path=/database/migrations/full_migration_file_name_migration.php` As for the `1050` error, post full migration file. – geertjanknapen May 03 '22 at 12:49
  • 3
    You seem to also have a `contracts` migration that didn't previously run via a migration (for whatever reason) but you already have the migration created. If this is only dev environment and you don't care about data loss try running `php artisan migrate:fresh`. **do not run this in production or if you want to keep the data already in the database** – apokryfos May 03 '22 at 12:50
  • @geertjanknapen I tried it but it returned this error what can cause this? https://ibb.co/Fzn13Bc – NervousDev May 03 '22 at 13:02
  • @apokryfos yes I guess I had that error before but I was able to migrate the table to the database. It is an dev environment but I had around 20-30 data at the `contacts` table. Is it clearing all the tables or all the database? – NervousDev May 03 '22 at 13:05
  • @geertjanknapen I edit the question and added everything on my migration file. – NervousDev May 03 '22 at 13:10
  • fresh migration would clear the database and run all migrations again so if you have things not created by migrations they will be lost. If you don't want that you can do `php artisan migrate:refresh` which will rollback and rerun all migrations but this assumes the `down` method in each migration is successful – apokryfos May 03 '22 at 13:49
  • @apokryfos Okay I tried `php artisan migrate:fresh` but it return this error. I don't know what is wrong... https://ibb.co/xsqCZ99 – NervousDev May 03 '22 at 15:05

2 Answers2

3

so first change timestamp line and put this line:

$table->timestamp('email_verified_at')->nullable();

then in command line use:

php artisan migrate:fresh
a bcd
  • 188
  • 9
0
public function down()
{
    Schema::table('users', function($table) {
        $table->dropUnique(['email']);
        $table->dropColumn('email');
        $table->dropUnique(['username']);
        $table->dropColumn('username');
    });
    Schema::dropIfExists('users');
}

run-> php artisan migrate:refresh And don't forget before running add drop uniques at down method.