Laravel Database Schema, Nullable Foreign

I have these two database tables:

  • Custom tables
  • Partner tables

Custom tables will handle these kinds of information.

Schema::create('users', function (Blueprint $table) { $table->increments('id')->unique(); $table->string('email')->unique(); $table->string('username')->unique(); $table->string('password', 60); $table->string('photo')->nullable(); $table->integer('partner_id')->unsigned(); $table->foreign('partner_id')->references('id')->on('partners'); $table->rememberToken(); $table->timestamps(); }); 

While Partner Tables will contain all user meta-information, such as first and last name, etc.

 Schema::create('partners', function (Blueprint $table) { /** * Identity Columns */ $table->increments('id')->unique(); $table->string('first_name'); $table->string('middle_name')->nullable(); $table->string('last_name')->nullable(); $table->string('display_name')->nullable(); $table->string('email')->unique()->nullable(); $table->string('website')->nullable(); $table->string('phone')->nullable(); $table->string('mobile')->nullable(); $table->string('fax')->nullable(); $table->date('birthdate')->nullable(); $table->longText('bio')->nullable(); $table->string('lang')->nullable(); //Language /** * Address Columns */ $table->text('street')->nullable(); $table->text('street2')->nullable(); $table->integer('country_id')->unsigned(); // foreign $table->foreign('country_id')->references('id')->on('countries'); $table->integer('state_id')->unsigned(); // foreign $table->foreign('state_id')->references('id')->on('country_states'); $table->string('city')->nullable(); $table->string('district')->nullable(); $table->string('area')->nullable(); $table->string('zip')->nullable(); }); 

When a user registers on the site, I only need a few fields: username , email address , password , first name and last name . These are only required fields.

Thus, the information in the partner tables can be filled in later after the user completes registration on the site.

But due to the structure of the foreign key, I cannot continue further due to this error:

 SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mytable`.`tbl_partners`, CONSTRAINT `partners_country_id_foreign` FOREIGN KEY (`country_id`) REFERENCES `tbl_countries` (`id`)) (SQL: insert into `tbl_partners` (`first_name`, `last_name`, `display_name`, `email`, `updated_at`, `created_at`) values (Jack, Wilson, admin, admin@example.com , 2016-06-09 19:41:18, 2016-06-09 19:41:18)) 

I know that this is the reason for the country table, which is required in the partners table.
My question is: is there work so that I can fill out the country or any other data that is not required in the partners table, but save the external table layout for countries, states, etc.

+7
database php mysql laravel
source share
1 answer

Set country_id and state_id valid values. For example:

 $table->integer('country_id')->nullable()->unsigned(); $table->integer('state_id')->nullable()->unsigned(); 
+23
source share

All Articles