Boban Mladenovic
Boban Mladenovic

Reputation: 17

Laravel 5.6 migrations

I have a problem with migrations in a Laravel 5.6.

This is a problem:

My code in a Laravel:

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

class CreateCompaniesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->timestamps();
        });
    }

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

What i need to do?

Upvotes: 0

Views: 157

Answers (3)

munazzil
munazzil

Reputation: 107

You can use below command to drop every table and migrate before that you have to take a backup of your sql because it deletes every data

 php artisan migrate:fresh

Upvotes: 0

Mihiran Chathuranga
Mihiran Chathuranga

Reputation: 116

As shown in the error message "companies" table already exist. you can use following commands to resolve it.

php artisan migrate:rollback

php artisan migrate:reset

please refer the laravel documentation for more details.

Upvotes: 0

latr.88
latr.88

Reputation: 859

Table already exists as it is telling you in the message, you may need to drop it first or check that you're using the right database name in your ENV file. Also check you're not creating the table companies in a previous migration.

If you're trying to add fields to an existing table you don't need the create method but:

Schema::table("companies", function (Blueprint $table) {
        // The fields you need
});

Also for the foreign key is safer to use unsignedInteger as data type

Upvotes: 2

Related Questions