提交表单时抛出error:integrity constraint 违规:1452无法添加或更新子行:外键约束失败(portal\u job.applicator\u job,constraint applicator\u job\u applicator\u user\u id\u外键(applicator\u user\u id)引用delete cascade上的applicators(id))(sql:插入到applicator\u job(applicator\u user\u id,创建的\u at、作业\u id、更新的\u at)值(4,2018-10-01 23:46:56,2,2018-10-01 23:46:56))
如何在两个相关表中插入?在这种情况下,将是多对多关系。我不知道在mysql上创建关系是否会导致这个问题。。。如果你需要更多的信息,请告诉我。我不知道还能做什么,因为我已经尝试使用attach()来做了,但没有用。也有同样的错误。
这是为任务执行的代码。它首先将数据保存到applicators表中,然后应该插入到applicator表中,后者是中间表,但这部分不行。
public function store(Request $request, $id) {
$this->validate($request, [
'application_letter' => 'required'
]);
$applicant = new Applicant;
$applicant->application_letter= $request->input('application_letter');
$applicant->job_id = $request->input('job');
$applicant->state = 'pending';
$applicant->user_id = auth()->user()->id;
$applicant->save();
$applicant->jobs()->sync([$id],false);
return redirect("userdashboard");
}
这些是表之间关系的模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Applicant extends Model
{
protected $primaryKey = 'user_id';
public $incrementing = false;
function jobs() {
return $this->belongsToMany('App\Models\Job')->withTimeStamps();
}
function user() {
return $this->belongsTo('App\Models\User','user_id');
}
function profile() {
return $this->hasOne('App\Models\Profile', 'profile');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
function category() {
return $this->belongsTo('App\Models\JobCategory');
}
function user() {
return $this->belongsTo('App\Models\User','user_id');
}
function applicants() {
return $this->belongsToMany('App\Models\Applicant', 'applicant_job', 'job_id', 'applicant_user_id')->withTimeStamps();
}
}
这些是迁移,它们各自的外键用于这部分任务。
Schema::create('applicants', function (Blueprint $table) {
$table->increments('id');
$table->text('application_letter');
$table->string('state');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->integer('job_id')->unsigned();
$table->foreign('job_id')->references('id')
->on('jobs')
->onDelete('cascade');
$table->foreign('user_id')->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('applicant_job', function (Blueprint $table) {
$table->increments('id');
$table->integer('applicant_user_id')->unsigned();
$table->integer('job_id')->unsigned();
$table->timestamps();
$table->foreign('applicant_user_id')->references('id')
->on('applicants')
->onDelete('cascade');
$table->foreign('job_id')->references('id')
->on('jobs')
->onDelete('cascade');
});
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->decimal('payment');
$table->integer('category_id')->unsigned();
$table->string('position_type');
$table->string('duration_project');
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('category_id')->references('id')
->on('job_category')
->onDelete('cascade');
});
这是为一个小型作业门户网站所做的。如需更多信息,请随时询问。
1条答案
按热度按时间vcudknz31#
对于插入/保存,必须使用attach()$parenttable->jobs()->attach($arrayvariable);
更新使用:
sync();
对于删除多个:detach();