问题
I'm studying Laravel and working around in php artisan but I keep on getting this error:
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.url (SQL: insert into "profiles" ("title", "description", "user_id", "updated_at", "created_at") values (Cool, Desc, 1, 2020-03-12 02:03:16, 2020-03-12 02:03:16))'
I am attaching a Profile to a User and my User table has contents:
>> User::all()
=> Illuminate\Database\Eloquent\Collection {#2992
all: [
App\User {#2993
id: "1",
name: "Test User1",
email: "test1@test.com",
username: "test1",
email_verified_at: null,
created_at: "2020-03-12 02:01:08",
updated_at: "2020-03-12 02:01:08",
},
], }
This is what I entered in PHP Artisan
>>> $profile = new \App\Profile();
=> App\Profile {#2987}
>>> $profile->title = 'Cool Title';
=> "Cool Title"
>>> $profile->description = 'Desc';
=> "Desc"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
Profiles_table.php
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id')->nullable;
$table->unsignedBigInteger('user_id')->nullable;
$table->string('title')->nullable;
$table->text('description')->nullable;
$table->string('url')->nullable;
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('profiles');
}
}
Create_users_table.php
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken()->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I can't find the reason why it is failing. Hope someone can find out what is causing it. Thanks
回答1:
I think there is a typo on your 'profile' migration.
$table->string('url')->nullable;
to
$table->string('url')->nullable();
来源:https://stackoverflow.com/questions/60646976/php-error-in-laravel-while-inserting-data-via-sqlite