phplaravelforeign-keyslaravel-7artisan-migrate

foreign key not migrate. I have some problems with migrations. I already well migrate company table


This my code

Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->text('second_name');
            $table->string('emp_company')->unsigned();
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
            $table->foreign('emp_company')->references('company_name')->on('companies');
        });

here my error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'unsigned not null, email varchar(255) not null, phone varchar(255) not null,' at line 1 (SQL: create table employees (id bigint unsigned not null auto_increment primary key, first_name varchar(255) not null, second_name text not null, emp_company varchar(255) unsigned not null, email varchar(255) not null, phone varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

im stuck with this ..looking for sup Thank you.


Solution

  • i think your problem in this line:

     $table->string('emp_company')->unsigned();
    

    unsigned is used only for numeric columns, to force only positive numbers, there is no UNSIGNED for string column, it's meanless

    just remove ->unsigned() and your code should work ....