JsWizard
JsWizard

Reputation: 1749

SQLSTATE[23000]: Integrity constraint violation in Laravel

When I click submit(save) button for make a Job... then I got below exception.... (user_name field already posited as hidden in the job.create.blade view form)

Could you tell me where can I modify for fix? Thanks.

enter image description here

(3/3) QueryException

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (admin-db.jobs, CONSTRAINT jobs_customer_name_foreign FOREIGN KEY (customer_name) REFERENCES customers (customer_name)) (SQL: insert into jobs (user_name, customer_name, job_place, job_type, note_1, time_used, updated_at, created_at) values (John, 1, Kontor, Domene og Webhotell, asdf, , 2017-10-08 00:23:40, 2017-10-08 00:23:40))

timestamp_create_users_table.php

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';            
        $table->increments('id')->unsigned();

        $table->string('name')->index();
        $table->string('email');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

timestamp_create_customers_table.php

public function up()
{        
    Schema::create('customers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('id')->unsigned();

        $table->string('customer_name')->index();
        $table->string('customer_email');
        $table->timestamps();
        $table->softDeletes();
    });
}

timestamp_create_jobs_table.php

public function up()
{

    Schema::create('jobs', function (Blueprint $table) {
        $table->engine = 'InnoDB';

        $table->increments('id')->unsigned();

        $table->string('user_name')->index();
        $table->string('customer_name')->index();        
        $table->string('job_place');
        $table->string('job_type');
        $table->string('note_1');
        $table->time('time_used')->nullable();
        $table->timestamps();
        $table->softDeletes();

        $table->foreign('user_name')->nullable()->references('name')->on('users')->onDelete('cascade');
        $table->foreign('customer_name')->nullable()->references('customer_name')->on('customers')->onDelete('cascade');
    });
}

Model relationship define in: Job.php

public function users()
{
    return $this->belongsTo(User::class);
}

public function customers()
{
    return $this->belongsTo(Customer::class);
}

Model relationship define in: Customer.php

public function jobs()
{
    return $this->hasMany(Job::class);
}

Upvotes: 0

Views: 231

Answers (1)

JsWizard
JsWizard

Reputation: 1749

I got the answer by myself...

The problem was using in pluck method. below is well working after modified it.

<div class="form-group col-sm-6">
        {!! Form::label('customer_name', 'Customer Name:') !!}
        {!! Form::select('customer_name', $searchdata->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
</div>

Upvotes: 1

Related Questions