2

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: "[email protected]",
         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 tried to use fillable and nullable as well on the fields but it didn't help. Commented Mar 12, 2020 at 3:12

1 Answer 1

2

I think there is a typo on your 'profile' migration.

$table->string('url')->nullable;

to

$table->string('url')->nullable();
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I was actually smiling after reading your comment as I know that I made a silly (funny one for me) mistake so I tried it right away and it worked. Thanks a lot. An extra pair of eyes really means a lot.
We're all doing this kind of mistake :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.