SQLSTATE [23000]:Laravel

时间:2017-10-08 06:05:53

标签: mysql exception laravel-5

当我单击提交(保存)按钮以创建作业...然后我得到以下异常....( user_name 字段已经隐藏在 job.create中。刀片查看表单)

你能告诉我在哪里可以修改修复? 感谢。

enter image description here

(3/3)QueryException

  

SQLSTATE [23000]:完整性约束违规:1452无法添加或   更新子行:外键约束失败(admin-dbjobs,   约束jobs_customer_name_foreign外键(customer_name)   参考customerscustomer_name))(SQL:插入jobs   (user_namecustomer_namejob_placejob_typenote_1,   time_usedupdated_atcreated_at)值(John,1,Kontor,   Domene og Webhotell,asdf ,, 2017-10-08 00:23:40,2017-10-08   〇时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');
    });
}

模型关系定义于:Job.php

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

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

模型关系定义于:Customer.php

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

1 个答案:

答案 0 :(得分:1)

我自己得到了答案......

问题是在采摘方法中使用。 修改后,下面的工作正常。

<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>