Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

angelorigo's avatar

Integrity constraint violation

Hi,

I am following the chapter 9 at 30 days to learn laravel series, i have the employer_id colum at job_listings table .

I already have two records at the employer table , when i try to run php artisan tinker App\Models\Job::create(['employer_id'=>2, 'title'=>'Acme Director', 'salary'=>'$1,000,000']);

the message is :

Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: job_listings.employer_id (Connection: sqlite, SQL: insert into "job_listings" ("title", "salary", "updated_at", "created_at") values (Acme Director, $1,000,000, 2025-02-09 21:19:49, 2025-02-09 21:19:49)).

How can i fix and insert a record on job_listings table ?

0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message you're encountering indicates that the employer_id column in the job_listings table is not being populated, even though you're trying to set it. This could be due to a few reasons. Let's go through some steps to troubleshoot and resolve this issue:

  1. Check Migration File: Ensure that the employer_id column is correctly defined in your migration file for the job_listings table. It should be defined as a foreign key and should allow null values if you intend to insert records without an employer.

    Schema::create('job_listings', function (Blueprint $table) {
        $table->id();
        $table->foreignId('employer_id')->constrained()->onDelete('cascade');
        $table->string('title');
        $table->string('salary');
        $table->timestamps();
    });
    
  2. Check Model Fillable Property: Ensure that the employer_id is included in the $fillable property of your Job model. This allows mass assignment for that attribute.

    class Job extends Model
    {
        protected $fillable = ['employer_id', 'title', 'salary'];
    }
    
  3. Database Connection: Since you're using SQLite, ensure that your database connection is correctly configured in your .env file and that the database file exists.

  4. Run Migrations: If you made changes to the migration file, you might need to refresh your database. Be cautious, as this will delete all data in your tables.

    php artisan migrate:fresh
    
  5. Check Tinker Command: Ensure that the command you are using in Tinker is correct. It seems like you are using the correct syntax, but double-check for any typos or syntax errors.

    php artisan tinker
    

    Then, in Tinker:

    App\Models\Job::create(['employer_id' => 2, 'title' => 'Acme Director', 'salary' => '$1,000,000']);
    

By following these steps, you should be able to resolve the integrity constraint violation and successfully insert a record into the job_listings table. If the problem persists, double-check the database schema and ensure that the employer_id column is correctly set up as a foreign key.

1 like
angelorigo's avatar

i just add to the fillable property at job class and it work. protected $fillable = ['title', 'salary', 'employer_id'];

tykus's avatar

@angelorigo whenever you are confident enough, you can add to the AppServiceProvider the following

\Illuminate\Database\Eloquent\Model::unguard();

Note, that this disables Mass Assignment Protection for all of your models, but is more convenient than managing the $fillable property on each Model class. You're effectively taking the training wheels off, but this is mitigated by passing only Validated Data from the Request

1 like

Please or to participate in this conversation.