具有多个关系的Laravel-5保存实体

具有多个关系的Laravel-5保存实体,laravel,eloquent,laravel-5,Laravel,Eloquent,Laravel 5,我在Laravel 5项目中有三个实体项目、员工和客户。项目与客户和员工之间存在多对一关系。 我已声明如下所示的关系 在项目模型中: /** * Fetch the Staff entity associated with a project * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function projectLead() { return $this->belong

我在Laravel 5项目中有三个实体项目、员工和客户。项目与客户和员工之间存在多对一关系。 我已声明如下所示的关系

在项目模型中:

/**
 * 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');
   }
用于保存项目的My 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))

我在这个错误上坚持了相当长的时间。非常感谢您提供的任何帮助。

员工模式更改为此

 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');
}

你能粘贴上面的表约束吗?我已经添加了迁移脚本add staff\u id return$this->hasMany('App\Project','Project\u leader\u id','staff\u id')@普拉维什:这有帮助吗?