我在Laravel 5项目中有三个实体Project,Staff和Client。项目与客户和员工有多对一的关系。 我已经宣布了下面给出的关系。
在项目模型中:
/**
* Fetch the Staff entity associated with a project
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function projectLead()
{
return $this->belongsTo('App\Staff','project_leader_id');
}
/**
* Fetch the Client entity associated with a project
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function client()
{
return $this->belongsTo('App\Client','client_id');
}
员工模式:
public function leadProjects()
{
return $this->hasMany('App\Project','project_leader_id');
}
在客户端模型中:
private function projects()
{
return $this->hasMany('App\Project');
}
用于保存项目的ProjectController操作:
public function store( CreateProjectRequest $request)
{
$input = $request->all();
$project = new Project();
$teamLead = Staff::find($input['project_lead_id']);
$client = Client::find($input['project_lead_id']);
$project->name = $input['name'];
$project->active = $input['active'];
$project->date_setup = $input['date_setup'];
$project->status = $input['status'];
$project->hourly_rate_default = $input['hourly_rate_default'];
$project->projectLead()->associate($teamLead);
$project->client()->associate($client);
$project->save();
}
迁移:
Schema::create('project', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->integer('project_id', true);
$table->string('name', 100);
$table->boolean('active');
$table->date('date_setup');
$table->boolean('status');
$table->float('hourly_rate_default', 10, 0);
$table->integer('project_leader_id');
$table->foreign('project_leader_id')
->references('staff_id')->on('staff')->onDelete('cascade');
$table->timestamps();
$table->integer('client_id');
$table->foreign('client_id')
->references('client_id')->on('client')->onDelete('cascade');
});
但是,当我尝试保存新项目时,我收到错误。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`erplite_portal`.`project`, CONSTRAINT `project_project_leader_id_foreign` FOREIGN KEY (`project_leader_id`) REFERENCES `staff` (`staff_id`) ON DELETE CASCADE) (SQL: insert into `project` (`name`, `active`, `date_setup`, `status`, `hourly_rate_default`, `project_leader_id`, `client_id`, `updated_at`, `created_at`) values (Aida Shaghayegh Irajinia, 1, 12-01-81, 2, 12, {"staff_id":"1","name":"Praveesh A","date_joined":"2015-04-01","date_resigned":"0000-00-00","staff_role":"development","password":"praveesh","email":"praveesh@lbit.in","is_master":"1","phone":"9446814101","phone_alternate":"9448162992","profile_picture":"","active":"1"}, {"client_id":"1","active":"1","name":"Lbit","address":"Lbit Ekm","country_id":"91","phone":"9446814101","date_registered":"2015-04-01","project_lead_id":"1","billing_account_id":"1","credit_limit":"100","invoice_day":"0","invoice_due_days":"15","require_prepayment":"1","deduct_cc_fee_percentage":"0","hourly_rate":"150","currency_id":"91","recommended_prepayment_hours":"15","invoice_automatically":"1","invoice_projects_together":"1","invoice_representative_id":"1"}, 2015-04-27 01:44:13, 2015-04-27 01:44:13))
我已经坚持这个错误了相当长的时间。任何帮助都非常感谢。
答案 0 :(得分:1)
在员工模型中更改为
public function leadProjects()
{
return $this->hasMany('App\Project','project_leader_id','staff_id');
}
在项目模型中
public function projectLead()
{
return $this->belongsTo('App\Staff','project_leader_id','staff_id');
}